Files
using.cash/Database/Database-Permissions.cs
T

65 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using Swarmops.Basic.Types;
using Swarmops.Basic.Types.Security;
using Swarmops.Common.Enums;
namespace Swarmops.Database
{
public partial class SwarmDb
{
public BasicPermission[] GetPermissionsTable()
{
List<BasicPermission> result = new List<BasicPermission>();
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand ("Select RoleType, PermissionType from PermissionSpecifications",
connection);
using (DbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
try
{
result.Add (new BasicPermission (
(RoleType) (Enum.Parse (typeof (RoleType), reader.GetString (0), true)),
(Permission) (Enum.Parse (typeof (Permission), reader.GetString (1), true))
)
);
}
catch (ArgumentException)
{
// Ignore if the enums are not found. v4/v5 conflict.
// TODO: alert about this.
}
}
}
}
return result.ToArray();
}
public void StoreOnePermission (int RoleTypeId, int PermissionId, bool allow)
{
using (DbConnection connection = GetMySqlDbConnection())
{
connection.Open();
DbCommand command = GetDbCommand ("StoreOnePermission", connection);
command.CommandType = CommandType.StoredProcedure;
AddParameterWithName (command, "p_RoleType", ((RoleType) RoleTypeId).ToString());
AddParameterWithName (command, "p_PermissionType", ((Permission) PermissionId).ToString());
AddParameterWithName (command, "p_allow", allow ? 1 : 0);
command.ExecuteNonQuery();
}
}
}
}