273 lines
7.8 KiB
C#
273 lines
7.8 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using xCommons.Extensions;
|
|
using xExceptions.Constants;
|
|
using xIdentityModels;
|
|
|
|
namespace xIds.Providers
|
|
{
|
|
public partial class XIdentityManager
|
|
{
|
|
//
|
|
#region Role Actions ...
|
|
/// <summary>
|
|
/// Check a Role exists or not
|
|
/// </summary>
|
|
/// <param name="roleName">role name</param>
|
|
/// <returns>a boolean value</returns>
|
|
public async Task<bool> IsRoleExistsAsync(
|
|
string roleName
|
|
)
|
|
{
|
|
//
|
|
if (roleName.IsNullOrEmpty())
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
return await RoleManager.RoleExistsAsync(roleName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a new Role
|
|
/// </summary>
|
|
/// <param name="roleName">role name</param>
|
|
/// <returns>an instance of <see>IdentityResult</see></returns>
|
|
public async Task<IdentityResult> CreateRoleAsync(
|
|
string roleName
|
|
)
|
|
{
|
|
//
|
|
var isExists = await RoleManager
|
|
.RoleExistsAsync(roleName);
|
|
if (isExists)
|
|
{
|
|
XException.Duplicate.Throw();
|
|
}
|
|
|
|
//
|
|
var result = await RoleManager
|
|
.CreateAsync(new IdentityRole(roleName));
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get a Role
|
|
/// </summary>
|
|
/// <param name="roleName">role name</param>
|
|
/// <returns>an instance of <see>IdentityRole</see></returns>
|
|
public async Task<IdentityRole> GetRoleAsync(
|
|
string roleName
|
|
)
|
|
{
|
|
//
|
|
var isExists = await IsRoleExistsAsync(roleName);
|
|
if (!isExists)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
var result = await RoleManager
|
|
.FindByNameAsync(roleName);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove a User From Role
|
|
/// </summary>
|
|
/// <param name="user">an instance of <see>XUser</see></param>
|
|
/// <param name="roleName">role name</param>
|
|
/// <returns>an instance of <see>IdentityResult</see></returns>
|
|
public async Task<IdentityResult> RemoveFromRoleAsync(
|
|
XUser user,
|
|
string roleName
|
|
)
|
|
{
|
|
return await UserManager.RemoveFromRoleAsync(user, roleName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove a User From Roles
|
|
/// </summary>
|
|
/// <param name="user">an instance of <see>XUser</see></param>
|
|
/// <param name="roleNames">a collection of role names</param>
|
|
/// <returns>an instance of <see>IdentityResult</see></returns>
|
|
public async Task<IdentityResult> RemoveFromRolesAsync(
|
|
XUser user,
|
|
IEnumerable<string> roleNames
|
|
)
|
|
{
|
|
return await UserManager.RemoveFromRolesAsync(user, roleNames);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Assign a User to Specific Role
|
|
/// </summary>
|
|
/// <param name="user">an instance of <see>XUser</see></param>
|
|
/// <param name="roleName">role name</param>
|
|
/// <returns>an instance of <see>IdentityResult</see></returns>
|
|
public async Task<IdentityResult> AddUserToRoleAsync(
|
|
XUser user,
|
|
string roleName
|
|
)
|
|
{
|
|
//
|
|
if (user == null ||
|
|
roleName.IsNullOrEmpty())
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
var userSelectByParam = GetUserSelectByParam(user);
|
|
var isExistsUser = await IsUserExistsAsync(userSelectByParam);
|
|
var isExistsRole = await IsRoleExistsAsync(roleName);
|
|
if (!isExistsUser || !isExistsRole)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
var result = await UserManager
|
|
.AddToRoleAsync(user, roleName);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve Role Name by it's ID
|
|
/// </summary>
|
|
/// <param name="roleId">role id</param>
|
|
/// <returns>role name as string</returns>
|
|
public async Task<string> GetRoleNameAsync(
|
|
string roleId
|
|
)
|
|
{
|
|
//
|
|
// Validate Args ...
|
|
if (roleId.IsNullOrEmpty())
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
var role = await RoleManager
|
|
.FindByIdAsync(roleId);
|
|
|
|
//
|
|
return role.Name.ToNormalString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get a List Of User Roles
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">user identifier</param>
|
|
/// <param name="checkCanLoginPolicies"></param>
|
|
/// <param name="checkIsBanned"></param>
|
|
/// <returns>a collection of role names</returns>
|
|
public async Task<IEnumerable<string>> GetRoleNamesAsync(
|
|
string userSelectByParam,
|
|
bool checkCanLoginPolicies = true,
|
|
bool checkIsBanned = true
|
|
)
|
|
{
|
|
//
|
|
// Validate Args ...
|
|
if (userSelectByParam.IsNullOrEmpty())
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
var user = await ValidateUserExistsAndRetrieve(
|
|
userSelectByParam,
|
|
containDetails: true,
|
|
checkCanLoginPolicies: checkCanLoginPolicies,
|
|
checkIsBanned: checkIsBanned
|
|
);
|
|
|
|
//
|
|
var result = new List<string>();
|
|
foreach (var role in user.Roles)
|
|
{
|
|
//
|
|
var roleName = await GetRoleNameAsync(role.RoleId);
|
|
roleName = roleName.ToNormalString();
|
|
|
|
//
|
|
result.Add(roleName);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check a User is Adminr not
|
|
/// </summary>
|
|
/// <param name="userSelectByParam">user identifier</param>
|
|
/// <returns>a boolean value</returns>
|
|
public async Task<bool> IsAdmin(
|
|
string userSelectByParam
|
|
)
|
|
{
|
|
//
|
|
// Validate Args ...
|
|
ValidationProvider.NotEmpty(userSelectByParam);
|
|
|
|
//
|
|
var user = await ValidateUserExistsAndRetrieve(
|
|
userSelectByParam,
|
|
containDetails: true,
|
|
checkCanLoginPolicies: true);
|
|
|
|
//
|
|
var result = await IsAdmin(user);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check a User is Adminr not
|
|
/// </summary>
|
|
/// <param name="user">an instance of <see>XUser</see></param>
|
|
/// <returns>a boolean value</returns>
|
|
public async Task<bool> IsAdmin(
|
|
XUser user
|
|
)
|
|
{
|
|
//
|
|
// Validate Args ...
|
|
ValidationProvider.NotNull(user);
|
|
ValidationProvider.NotEmpty(user.Id);
|
|
|
|
//
|
|
// Retrieve Admin Role ...
|
|
// TODO: Fix this ...
|
|
var adminRole = "admin";
|
|
// XUserRole.Admin
|
|
// .GetStringValue ()
|
|
// .ToNormalString ();
|
|
|
|
//
|
|
// Check Admin Role Exists in User Roles or not ...
|
|
var userRoleNames = await GetRoleNamesAsync(user.Id);
|
|
var isContainsAdminRole = userRoleNames.Contains(adminRole);
|
|
|
|
//
|
|
// Return Result ...
|
|
return isContainsAdminRole;
|
|
}
|
|
#endregion
|
|
}
|
|
} |