Adds group manager, editable only by DB program.

This commit is contained in:
Twitchy 2011-07-11 20:33:03 +12:00
parent b5ca97ae35
commit 0997b5bd5d
4 changed files with 121 additions and 76 deletions

View file

@ -11,14 +11,123 @@ namespace TShockAPI.DB
{ {
private IDbConnection database; private IDbConnection database;
public List<Group> groups = new List<Group>();
public GroupManager(IDbConnection db) public GroupManager(IDbConnection db)
{ {
database = db; database = db;
using (var com = database.CreateCommand())
{
com.CommandText =
"CREATE TABLE IF NOT EXISTS 'GroupList' ('ID' INTEGER PRIMARY KEY UNIQUE, 'GroupName' TEXT UNIQUE, 'Commands' TEXT, 'OrderBy' TEXT);";
com.ExecuteNonQuery();
com.CommandText = "INSERT OR IGNORE INTO GroupList (GroupName, Commands, OrderBy) VALUES (@groupname, @commands, @order);";
com.AddParameter("@groupname", "trustedadmin");
com.AddParameter("@commands", "maintenance,cfg,butcher,cheat,immunetoban,ignorecheatdetection,ignoregriefdetection,usebanneditem");
com.AddParameter("@order", "1");
com.ExecuteNonQuery();
com.Parameters.Clear();
com.CommandText = "INSERT OR IGNORE INTO GroupList (GroupName, Commands, OrderBy) VALUES (@groupname, @commands, @order);";
com.AddParameter("@groupname", "admin");
com.AddParameter("@commands", "ban,unban,whitelist,causeevents,spawnboss,spawnmob,managewarp,time,tp,pvpfun,kill,logs,immunetokick,tphere");
com.AddParameter("@order", "2");
com.ExecuteNonQuery();
com.Parameters.Clear();
com.CommandText = "INSERT OR IGNORE INTO GroupList (GroupName, Commands, OrderBy) VALUES (@groupname, @commands, @order);";
com.AddParameter("@groupname", "newadmin");
com.AddParameter("@commands", "kick,editspawn,reservedslot");
com.AddParameter("@order", "3");
com.ExecuteNonQuery();
com.Parameters.Clear();
com.CommandText = "INSERT OR IGNORE INTO GroupList (GroupName, Commands, OrderBy) VALUES (@groupname, @commands, @order);";
com.AddParameter("@groupname", "default");
com.AddParameter("@commands", "canwater,canlava,warp,manageusers");
com.AddParameter("@order", "4");
com.ExecuteNonQuery();
com.Parameters.Clear();
LoadPermisions();
}
} }
public bool GroupExists(string group) public bool GroupExists(string group)
{ {
return true; try
{
using (var com = database.CreateCommand())
{
com.CommandText = "SELECT * FROM Grouplist WHERE GroupName=@groupname";
com.AddParameter("@groupname", group);
using (var reader = com.ExecuteReader())
{
while (reader.Read())
if (reader.Get<string>("GroupName") == group)
return true;
}
}
}
catch (SqliteExecutionException ex)
{
Log.Error(ex.ToString());
}
return false;
}
public void LoadPermisions()
{
groups = new List<Group>();
groups.Add(new SuperAdminGroup());
try
{
using (var com = database.CreateCommand())
{
com.CommandText = "SELECT * FROM Grouplist;";
using (var reader = com.ExecuteReader())
{
while (reader.Read())
{
Group group = null;
string groupname = reader.Get<string>("GroupName");
int order = Int32.Parse(reader.Get<string>("OrderBy"));
group = new Group(groupname);
group.Order = order;
//Inherit Given commands
foreach (string perm in reader.Get<string>("Commands").Split(','))
{
group.AddPermission(perm);
}
groups.Add(group);
}
}
//Inherit all commands from group below in order, unless Order is 0 (unique groups anyone)
foreach (Group group in groups)
{
if (group.Order != 0 && group.Order < groups.Count)
{
for (int i = group.Order + 1; i < groups.Count; i++)
{
for (int j = 0; j < groups[i].permissions.Count; j++)
{
group.AddPermission(groups[i].permissions[j]);
}
}
}
}
}
}
catch (SqliteExecutionException ex)
{
Log.Error(ex.ToString());
}
} }
} }
} }

View file

@ -22,11 +22,12 @@ namespace TShockAPI
{ {
public class Group public class Group
{ {
private readonly List<string> permissions = new List<string>(); public readonly List<string> permissions = new List<string>();
private readonly List<string> negatedpermissions = new List<string>(); private readonly List<string> negatedpermissions = new List<string>();
public string Name { get; protected set; } public string Name { get; protected set; }
public Group Parent { get; protected set; } public Group Parent { get; protected set; }
public int Order { get; set; }
public Group(string groupname, Group parentgroup = null) public Group(string groupname, Group parentgroup = null)
{ {

View file

@ -152,7 +152,7 @@
</PropertyGroup> </PropertyGroup>
<ProjectExtensions> <ProjectExtensions>
<VisualStudio> <VisualStudio>
<UserProperties BuildVersion_UpdateAssemblyVersion="True" BuildVersion_UpdateFileVersion="True" BuildVersion_BuildAction="Both" BuildVersion_BuildVersioningStyle="None.None.None.MonthAndDayStamp" BuildVersion_StartDate="2011/6/17" BuildVersion_IncrementBeforeBuild="False" /> <UserProperties BuildVersion_IncrementBeforeBuild="False" BuildVersion_StartDate="2011/6/17" BuildVersion_BuildVersioningStyle="None.None.None.MonthAndDayStamp" BuildVersion_BuildAction="Both" BuildVersion_UpdateFileVersion="True" BuildVersion_UpdateAssemblyVersion="True" />
</VisualStudio> </VisualStudio>
</ProjectExtensions> </ProjectExtensions>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View file

@ -30,7 +30,7 @@ namespace TShockAPI
internal class Tools internal class Tools
{ {
public static Random Random = new Random(); public static Random Random = new Random();
private static List<Group> groups = new List<Group>(); //private static List<Group> groups = new List<Group>();
/// <summary> /// <summary>
/// Provides the real IP address from a RemoteEndPoint string that contains a port and an IP /// Provides the real IP address from a RemoteEndPoint string that contains a port and an IP
@ -446,69 +446,6 @@ namespace TShockAPI
tr.Close(); tr.Close();
} }
public static void LoadGroups()
{
groups = new List<Group>();
groups.Add(new SuperAdminGroup());
StreamReader sr = new StreamReader(FileTools.GroupsPath);
string data = sr.ReadToEnd();
data = data.Replace("\r", "");
string[] lines = data.Split('\n');
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].StartsWith("#"))
{
continue;
}
string[] args = lines[i].Split(' ');
if (args.Length < 2)
{
continue;
}
string name = args[0];
string parent = args[1];
Group group = null;
if (parent.Equals("null"))
{
group = new Group(name);
}
else
{
for (int j = 0; j < groups.Count; j++)
{
if (groups[j].Name.Equals(parent))
{
group = new Group(name, groups[j]);
break;
}
}
}
if (group == null)
{
throw new Exception("Something in the groups.txt is fucked up");
}
else
{
for (int j = 2; j < args.Length; j++)
{
string permission = args[j];
if (permission.StartsWith("!"))
{
group.NegatePermission(args[j].Replace("!", ""));
}
else
{
group.AddPermission(args[j]);
}
}
}
groups.Add(group);
}
sr.Close();
}
/// <summary> /// <summary>
/// Returns a Group from the name of the group /// Returns a Group from the name of the group
/// </summary> /// </summary>
@ -516,24 +453,22 @@ namespace TShockAPI
public static Group GetGroup(string groupName) public static Group GetGroup(string groupName)
{ {
//first attempt on cached groups //first attempt on cached groups
for (int i = 0; i < groups.Count; i++) for (int i = 0; i < TShock.Groups.groups.Count; i++)
{ {
if (groups[i].Name.Equals(groupName)) if (TShock.Groups.groups[i].Name.Equals(groupName))
{ {
return groups[i]; return TShock.Groups.groups[i];
} }
} }
//shit, it didnt work, reload and try again //shit, it didnt work, reload and try again
LoadGroups(); TShock.Groups.LoadPermisions();
for (int i = 0; i < TShock.Groups.groups.Count; i++)
for (int i = 0; i < groups.Count; i++)
{ {
if (groups[i].Name.Equals(groupName)) if (TShock.Groups.groups[i].Name.Equals(groupName))
{ {
return groups[i]; return TShock.Groups.groups[i];
} }
} }
return new Group("null"); return new Group("null");
} }