Adds group manager, editable only by DB program.
This commit is contained in:
parent
b5ca97ae35
commit
0997b5bd5d
4 changed files with 121 additions and 76 deletions
|
|
@ -11,14 +11,123 @@ namespace TShockAPI.DB
|
|||
{
|
||||
private IDbConnection database;
|
||||
|
||||
public List<Group> groups = new List<Group>();
|
||||
|
||||
public GroupManager(IDbConnection 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)
|
||||
{
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue