Merge pull request #377 from stevenh/upstream

A number of small commits required for RestAPI fixes / enhancments
This commit is contained in:
Lucas Nicodemus 2012-02-13 16:54:29 -08:00
commit d3c1224ee7
8 changed files with 138 additions and 35 deletions

View file

@ -92,10 +92,6 @@ throw new Exception("Could not find a database library (probably Sqlite3.dll)");
public Ban GetBanByName(string name, bool casesensitive = true)
{
if (!TShock.Config.EnableBanOnUsernames)
{
return null;
}
try
{
var namecol = casesensitive ? "Name" : "UPPER(Name)";
@ -127,11 +123,15 @@ throw new Exception("Could not find a database library (probably Sqlite3.dll)");
return false;
}
public bool RemoveBan(string ip)
public bool RemoveBan(string match, bool byName = false, bool casesensitive = true)
{
try
{
return database.Query("DELETE FROM Bans WHERE IP=@0", ip) != 0;
if (!byName)
return database.Query("DELETE FROM Bans WHERE IP=@0", match) != 0;
var namecol = casesensitive ? "Name" : "UPPER(Name)";
return database.Query("DELETE FROM Bans WHERE " + namecol + "=@0", casesensitive ? match : match.ToUpper()) != 0;
}
catch (Exception ex)
{

View file

@ -16,6 +16,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
@ -24,7 +25,7 @@ using MySql.Data.MySqlClient;
namespace TShockAPI.DB
{
public class GroupManager
public class GroupManager : IEnumerable<Group>
{
private IDbConnection database;
@ -64,21 +65,42 @@ namespace TShockAPI.DB
if (group == "superadmin")
return true;
return groups.Any(g => g.Name.Equals(group));
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<Group> GetEnumerator()
{
return groups.GetEnumerator();
}
public Group GetGroupByName(string name)
{
var ret = groups.Where(g => g.Name == name);
return 1 == ret.Count() ? ret.ElementAt(0) : null;
}
/// <summary>
/// Adds group with name and permissions if it does not exist.
/// </summary>
/// <param name="name">name of group</param>
/// <param name="parentname">parent of group</param>
/// <param name="permissions">permissions</param>
public String AddGroup(String name, string parentname, String permissions, String chatcolor)
/// <param name="chatcolor">chatcolor</param>
/// <param name="exceptions">exceptions true indicates use exceptions for errors false otherwise</param>
public String AddGroup(String name, string parentname, String permissions, String chatcolor, bool exceptions = false)
{
String message = "";
if (GroupExists(name))
{
if (exceptions)
throw new GroupExistsException(name);
return "Error: Group already exists. Use /modGroup to change permissions.";
}
var group = new Group(name, null, chatcolor);
group.permissions.Add(permissions);
@ -88,6 +110,8 @@ namespace TShockAPI.DB
if (parent == null)
{
var error = "Invalid parent {0} for group {1}".SFormat(group.Name, parentname);
if (exceptions)
throw new GroupManagerException(error);
Log.ConsoleError(error);
return error;
}
@ -98,9 +122,12 @@ namespace TShockAPI.DB
? "INSERT OR IGNORE INTO GroupList (GroupName, Parent, Commands, ChatColor) VALUES (@0, @1, @2, @3);"
: "INSERT IGNORE INTO GroupList SET GroupName=@0, Parent=@1, Commands=@2, ChatColor=@3";
if (database.Query(query, name, parentname, permissions, chatcolor) == 1)
{
message = "Group " + name + " has been created successfully.";
groups.Add(group);
groups.Add(group);
}
else if (exceptions)
throw new GroupManagerException("Failed to add group '" + name + "'");
return message;
}
@ -115,15 +142,23 @@ namespace TShockAPI.DB
return AddGroup(name, parent, permissions, "255,255,255");
}
public String DeleteGroup(String name)
public String DeleteGroup(String name, bool exceptions = false)
{
String message = "";
if (!GroupExists(name))
{
if (exceptions)
throw new GroupNotExistException(name);
return "Error: Group doesn't exists.";
}
if (database.Query("DELETE FROM GroupList WHERE GroupName=@0", name) == 1)
{
message = "Group " + name + " has been deleted successfully.";
groups.Remove(TShock.Utils.GetGroup(name));
groups.Remove(TShock.Utils.GetGroup(name));
}
else if (exceptions)
throw new GroupManagerException("Failed to delete group '" + name + "'");
return message;
}
@ -239,4 +274,36 @@ namespace TShockAPI.DB
}
}
}
}
[Serializable]
public class GroupManagerException : Exception
{
public GroupManagerException(string message)
: base(message)
{
}
public GroupManagerException(string message, Exception inner)
: base(message, inner)
{
}
}
[Serializable]
public class GroupExistsException : GroupManagerException
{
public GroupExistsException(string name)
: base("Group '" + name + "' already exists")
{
}
}
[Serializable]
public class GroupNotExistException : GroupManagerException
{
public GroupNotExistException(string name)
: base("Group '" + name + "' does not exist")
{
}
}
}

View file

@ -42,7 +42,7 @@ namespace TShockAPI.DB
var columns =
table.Columns.Select(
c =>
"'{0}' {1} {2} {3} {4}".SFormat(c.Name, DbTypeToString(c.Type, c.Length), c.Primary ? "PRIMARY KEY" : "",
"'{0}' {1} {2} {3} {4} {5}".SFormat(c.Name, DbTypeToString(c.Type, c.Length), c.Primary ? "PRIMARY KEY" : "",
c.AutoIncrement ? "AUTOINCREMENT" : "", c.NotNull ? "NOT NULL" : "",
c.Unique ? "UNIQUE" : ""));
return "CREATE TABLE '{0}' ({1})".SFormat(table.Name, string.Join(", ", columns));
@ -198,7 +198,7 @@ namespace TShockAPI.DB
var columns =
table.Columns.Select(
c =>
"{0} {1} {2} {3}".SFormat(c.Name, DbTypeToString(c.Type, c.Length), c.Primary ? "PRIMARY KEY" : "",
"{0} {1} {2} {3} {4}".SFormat(c.Name, DbTypeToString(c.Type, c.Length), c.Primary ? "PRIMARY KEY" : "",
c.AutoIncrement ? "AUTO_INCREMENT" : "", c.NotNull ? "NOT NULL" : ""));
var uniques = table.Columns.Where(c => c.Unique).Select(c => c.Name);
return "CREATE TABLE {0} ({1} {2})".SFormat(table.Name, string.Join(", ", columns),

View file

@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Data;
using System.IO;
using System.Collections.Generic;
using MySql.Data.MySqlClient;
namespace TShockAPI.DB
@ -50,20 +51,22 @@ namespace TShockAPI.DB
/// <param name="user">User user</param>
public void AddUser(User user)
{
if (!TShock.Groups.GroupExists(user.Group))
throw new GroupNotExistsException(user.Group);
int ret;
try
{
if (!TShock.Groups.GroupExists(user.Group))
throw new GroupNotExistsException(user.Group);
if (
database.Query("INSERT INTO Users (Username, Password, UserGroup, IP) VALUES (@0, @1, @2, @3);", user.Name,
TShock.Utils.HashPassword(user.Password), user.Group, user.Address) < 1)
throw new UserExistsException(user.Name);
ret = database.Query("INSERT INTO Users (Username, Password, UserGroup, IP) VALUES (@0, @1, @2, @3);", user.Name,
TShock.Utils.HashPassword(user.Password), user.Group, user.Address);
}
catch (Exception ex)
{
throw new UserManagerException("AddUser SQL returned an error", ex);
throw new UserManagerException("AddUser SQL returned an error (" + ex.Message + ")", ex);
}
if (1 > ret)
throw new UserExistsException(user.Name);
}
/// <summary>
@ -254,14 +257,7 @@ namespace TShockAPI.DB
using (var reader = result)
{
if (reader.Read())
{
user.ID = reader.Get<int>("ID");
user.Group = reader.Get<string>("Usergroup");
user.Password = reader.Get<string>("Password");
user.Name = reader.Get<string>("Username");
user.Address = reader.Get<string>("IP");
return user;
}
return LoadUserFromResult(user, result);
}
}
catch (Exception ex)
@ -270,6 +266,37 @@ namespace TShockAPI.DB
}
throw new UserNotExistException(string.IsNullOrEmpty(user.Address) ? user.Name : user.Address);
}
public List<User> GetUsers()
{
try
{
List<User> users = new List<User>();
using (var reader = database.QueryReader("SELECT * FROM Users"))
{
while (reader.Read())
{
users.Add(LoadUserFromResult(new User(), reader));
}
return users;
}
}
catch (Exception ex)
{
Log.Error(ex.ToString());
}
return null;
}
private User LoadUserFromResult(User user, QueryResult result)
{
user.ID = result.Get<int>("ID");
user.Group = result.Get<string>("Usergroup");
user.Password = result.Get<string>("Password");
user.Name = result.Get<string>("Username");
user.Address = result.Get<string>("IP");
return user;
}
}
public class User

View file

@ -44,6 +44,11 @@ namespace TShockAPI
byte.TryParse(chatcolor.Split(',')[2], out B);
}
public string ChatColor()
{
return string.Format("{0}{1}{2}", R.ToString("X2"), G.ToString("X2"), B.ToString("X2"));
}
public virtual bool HasPermission(string permission)
{
var cur = this;

View file

@ -41,7 +41,7 @@ namespace Rests
set { this["response"] = value; }
}
public RestObject(string status)
public RestObject(string status = "200")
{
Status = status;
}

View file

@ -210,6 +210,9 @@ namespace TShockAPI
Commands.InitCommands();
//RconHandler.StartThread();
if (Config.RestApiEnabled)
RestApi.Start();
if (Config.BufferPackets)
PacketBuffer = new PacketBufferer();
@ -434,8 +437,6 @@ namespace TShockAPI
AuthToken = 0;
}
Regions.ReloadAllRegions();
if (Config.RestApiEnabled)
RestApi.Start();
StatTracker.CheckIn();
FixChestStacks();

View file

@ -205,6 +205,9 @@ namespace TShockAPI
public List<TSPlayer> FindPlayer(string ply)
{
var found = new List<TSPlayer>();
// Avoid errors caused by null search
if (null == ply)
return found;
ply = ply.ToLower();
foreach (TSPlayer player in TShock.Players)
{