-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ade60c
commit 3d2f946
Showing
88 changed files
with
6,190 additions
and
127 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using gaseous_server.Classes; | ||
using Microsoft.AspNetCore.Identity; | ||
|
||
namespace Authentication | ||
{ | ||
/// <summary> | ||
/// Class that implements the ASP.NET Identity | ||
/// IRole interface | ||
/// </summary> | ||
public class ApplicationRole : IdentityRole | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using gaseous_server.Classes; | ||
using Microsoft.AspNetCore.Identity; | ||
using System; | ||
|
||
namespace Authentication | ||
{ | ||
/// <summary> | ||
/// Class that implements the ASP.NET Identity | ||
/// IUser interface | ||
/// </summary> | ||
public class ApplicationUser : IdentityUser | ||
{ | ||
public SecurityProfileViewModel SecurityProfile { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
using System; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using gaseous_server.Classes; | ||
using Microsoft.AspNetCore.Identity; | ||
using MySqlConnector; | ||
|
||
namespace Authentication | ||
{ | ||
/// <summary> | ||
/// Class that implements the key ASP.NET Identity role store iterfaces | ||
/// </summary> | ||
public class RoleStore : IQueryableRoleStore<ApplicationRole> | ||
{ | ||
private RoleTable roleTable; | ||
public Database Database { get; private set; } | ||
|
||
public IQueryable<ApplicationRole> Roles | ||
{ | ||
get | ||
{ | ||
List<ApplicationRole> roles = roleTable.GetRoles(); | ||
return roles.AsQueryable(); | ||
} | ||
} | ||
|
||
public RoleStore() | ||
{ | ||
Database = new Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString); | ||
roleTable = new RoleTable(Database); | ||
} | ||
|
||
/// <summary> | ||
/// Constructor that takes a MySQLDatabase as argument | ||
/// </summary> | ||
/// <param name="database"></param> | ||
public RoleStore(Database database) | ||
{ | ||
Database = database; | ||
roleTable = new RoleTable(database); | ||
} | ||
|
||
public Task<IdentityResult> CreateAsync(ApplicationRole role, CancellationToken cancellationToken) | ||
{ | ||
if (role == null) | ||
{ | ||
throw new ArgumentNullException("role"); | ||
} | ||
|
||
roleTable.Insert(role); | ||
|
||
return Task.FromResult<IdentityResult>(IdentityResult.Success); | ||
} | ||
|
||
public Task<IdentityResult> DeleteAsync(ApplicationRole role, CancellationToken cancellationToken) | ||
{ | ||
if (role == null) | ||
{ | ||
throw new ArgumentNullException("user"); | ||
} | ||
|
||
roleTable.Delete(role.Id); | ||
|
||
return Task.FromResult<IdentityResult>(IdentityResult.Success); | ||
} | ||
|
||
public Task<ApplicationRole> FindByIdAsync(string roleId, CancellationToken cancellationToken) | ||
Check warning on line 67 in gaseous-server/Classes/Auth/Classes/RoleStore.cs GitHub Actions / build
|
||
{ | ||
ApplicationRole result = roleTable.GetRoleById(roleId) as ApplicationRole; | ||
|
||
return Task.FromResult<ApplicationRole>(result); | ||
} | ||
|
||
public Task<bool> RoleExistsAsync(string roleId, CancellationToken cancellationToken) | ||
{ | ||
ApplicationRole? result = roleTable.GetRoleById(roleId) as ApplicationRole; | ||
|
||
if (result == null) | ||
{ | ||
return Task.FromResult<bool>(false); | ||
} | ||
else | ||
{ | ||
return Task.FromResult<bool>(true); | ||
} | ||
} | ||
|
||
public Task<ApplicationRole?> FindByNameAsync(string roleName, CancellationToken cancellationToken) | ||
{ | ||
ApplicationRole? result = roleTable.GetRoleByName(roleName) as ApplicationRole; | ||
|
||
return Task.FromResult<ApplicationRole?>(result); | ||
} | ||
|
||
public Task<IdentityResult> UpdateAsync(ApplicationRole role, CancellationToken cancellationToken) | ||
{ | ||
if (role == null) | ||
{ | ||
throw new ArgumentNullException("user"); | ||
} | ||
|
||
roleTable.Update(role); | ||
|
||
return Task.FromResult<IdentityResult>(IdentityResult.Success); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (Database != null) | ||
{ | ||
Database = null; | ||
} | ||
} | ||
|
||
public Task<string> GetRoleIdAsync(ApplicationRole role, CancellationToken cancellationToken) | ||
{ | ||
if (role != null) | ||
{ | ||
return Task.FromResult<string>(roleTable.GetRoleId(role.Name)); | ||
} | ||
|
||
return Task.FromResult<string>(null); | ||
} | ||
|
||
public Task<string?> GetRoleNameAsync(ApplicationRole role, CancellationToken cancellationToken) | ||
{ | ||
if (role != null) | ||
{ | ||
return Task.FromResult<string?>(roleTable.GetRoleName(role.Id)); | ||
} | ||
|
||
return Task.FromResult<string?>(null); | ||
} | ||
|
||
public Task SetRoleNameAsync(ApplicationRole role, string? roleName, CancellationToken cancellationToken) | ||
{ | ||
if (role == null) | ||
{ | ||
throw new ArgumentNullException("role"); | ||
} | ||
|
||
role.Name = roleName; | ||
roleTable.Update(role); | ||
|
||
return Task.FromResult<IdentityResult>(IdentityResult.Success); | ||
} | ||
|
||
public Task<string?> GetNormalizedRoleNameAsync(ApplicationRole role, CancellationToken cancellationToken) | ||
{ | ||
if (role != null) | ||
{ | ||
return Task.FromResult<string?>(roleTable.GetRoleName(role.Id)); | ||
} | ||
|
||
return Task.FromResult<string?>(null); | ||
} | ||
|
||
public Task SetNormalizedRoleNameAsync(ApplicationRole role, string? normalizedName, CancellationToken cancellationToken) | ||
{ | ||
if (role == null) | ||
{ | ||
throw new ArgumentNullException("role"); | ||
} | ||
|
||
role.Name = normalizedName; | ||
roleTable.Update(role); | ||
|
||
return Task.FromResult<IdentityResult>(IdentityResult.Success); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using gaseous_server.Classes; | ||
using Microsoft.AspNetCore.Identity; | ||
|
||
namespace Authentication | ||
{ | ||
/// <summary> | ||
/// Class that represents the Role table in the MySQL Database | ||
/// </summary> | ||
public class RoleTable | ||
{ | ||
private Database _database; | ||
|
||
/// <summary> | ||
/// Constructor that takes a MySQLDatabase instance | ||
/// </summary> | ||
/// <param name="database"></param> | ||
public RoleTable(Database database) | ||
{ | ||
_database = database; | ||
} | ||
|
||
/// <summary> | ||
/// Deltes a role from the Roles table | ||
/// </summary> | ||
/// <param name="roleId">The role Id</param> | ||
/// <returns></returns> | ||
public int Delete(string roleId) | ||
{ | ||
string commandText = "Delete from Roles where Id = @id"; | ||
Dictionary<string, object> parameters = new Dictionary<string, object>(); | ||
parameters.Add("@id", roleId); | ||
|
||
return (int)_database.ExecuteNonQuery(commandText, parameters); | ||
} | ||
|
||
/// <summary> | ||
/// Inserts a new Role in the Roles table | ||
/// </summary> | ||
/// <param name="roleName">The role's name</param> | ||
/// <returns></returns> | ||
public int Insert(ApplicationRole role) | ||
{ | ||
string commandText = "Insert into Roles (Id, Name) values (@id, @name)"; | ||
Dictionary<string, object> parameters = new Dictionary<string, object>(); | ||
parameters.Add("@name", role.Name); | ||
parameters.Add("@id", role.Id); | ||
|
||
return (int)_database.ExecuteNonQuery(commandText, parameters); | ||
} | ||
|
||
/// <summary> | ||
/// Returns a role name given the roleId | ||
/// </summary> | ||
/// <param name="roleId">The role Id</param> | ||
/// <returns>Role name</returns> | ||
public string? GetRoleName(string roleId) | ||
{ | ||
string commandText = "Select Name from Roles where Id = @id"; | ||
Dictionary<string, object> parameters = new Dictionary<string, object>(); | ||
parameters.Add("@id", roleId); | ||
|
||
DataTable table = _database.ExecuteCMD(commandText, parameters); | ||
|
||
if (table.Rows.Count == 0) | ||
{ | ||
return null; | ||
} | ||
else | ||
{ | ||
return (string)table.Rows[0][0]; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Returns the role Id given a role name | ||
/// </summary> | ||
/// <param name="roleName">Role's name</param> | ||
/// <returns>Role's Id</returns> | ||
public string? GetRoleId(string roleName) | ||
{ | ||
string? roleId = null; | ||
string commandText = "Select Id from Roles where Name = @name"; | ||
Dictionary<string, object> parameters = new Dictionary<string, object>() { { "@name", roleName } }; | ||
|
||
DataTable result = _database.ExecuteCMD(commandText, parameters); | ||
if (result.Rows.Count > 0) | ||
{ | ||
return Convert.ToString(result.Rows[0][0]); | ||
} | ||
|
||
return roleId; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the ApplicationRole given the role Id | ||
/// </summary> | ||
/// <param name="roleId"></param> | ||
/// <returns></returns> | ||
public ApplicationRole? GetRoleById(string roleId) | ||
{ | ||
var roleName = GetRoleName(roleId); | ||
ApplicationRole? role = null; | ||
|
||
if(roleName != null) | ||
{ | ||
role = new ApplicationRole(); | ||
role.Id = roleId; | ||
role.Name = roleName; | ||
role.NormalizedName = roleName.ToUpper(); | ||
} | ||
|
||
return role; | ||
|
||
} | ||
|
||
/// <summary> | ||
/// Gets the ApplicationRole given the role name | ||
/// </summary> | ||
/// <param name="roleName"></param> | ||
/// <returns></returns> | ||
public ApplicationRole? GetRoleByName(string roleName) | ||
{ | ||
var roleId = GetRoleId(roleName); | ||
ApplicationRole role = null; | ||
|
||
if (roleId != null) | ||
{ | ||
role = new ApplicationRole(); | ||
role.Id = roleId; | ||
role.Name = roleName; | ||
role.NormalizedName = roleName.ToUpper(); | ||
} | ||
|
||
return role; | ||
} | ||
|
||
public int Update(ApplicationRole role) | ||
{ | ||
string commandText = "Update Roles set Name = @name where Id = @id"; | ||
Dictionary<string, object> parameters = new Dictionary<string, object>(); | ||
parameters.Add("@id", role.Id); | ||
|
||
return (int)_database.ExecuteNonQuery(commandText, parameters); | ||
} | ||
|
||
public List<ApplicationRole> GetRoles() | ||
{ | ||
List<ApplicationRole> roles = new List<ApplicationRole>(); | ||
|
||
string commandText = "Select Name from Roles"; | ||
|
||
var rows = _database.ExecuteCMDDict(commandText); | ||
foreach(Dictionary<string, object> row in rows) | ||
{ | ||
ApplicationRole role = (ApplicationRole)Activator.CreateInstance(typeof(ApplicationRole)); | ||
role.Id = (string)row["Id"]; | ||
role.Name = (string)row["Name"]; | ||
role.NormalizedName = ((string)row["Name"]).ToUpper(); | ||
roles.Add(role); | ||
} | ||
|
||
return roles; | ||
} | ||
} | ||
} |
Oops, something went wrong.