214 lines
7.3 KiB
Plaintext
214 lines
7.3 KiB
Plaintext
@using Oqtane.Services
|
|
@using Oqtane.Modules
|
|
@using Oqtane.Models
|
|
@using Oqtane.Security
|
|
@using Oqtane.Shared
|
|
@namespace Oqtane.Modules.Controls
|
|
@inherits ModuleBase
|
|
@inject IRoleService RoleService
|
|
@inject IUserService UserService
|
|
|
|
@if (roles != null)
|
|
{
|
|
<br />
|
|
<table class="table">
|
|
<tbody>
|
|
<tr>
|
|
<th>Role</th>
|
|
@foreach (PermissionString permission in permissions)
|
|
{
|
|
<th style="text-align: center;">@permission.PermissionName @EntityName</th>
|
|
}
|
|
</tr>
|
|
@foreach (Role role in roles)
|
|
{
|
|
<tr>
|
|
<td>@role.Name</td>
|
|
@foreach (PermissionString permission in permissions)
|
|
{
|
|
var p = permission;
|
|
<td style="text-align: center;">
|
|
<TriStateCheckBox Value=@GetPermissionValue(p.Permissions, role.Name) Disabled=@GetPermissionDisabled(role.Name) OnChange="@(e => PermissionChanged(e, p.PermissionName, role.Name))" />
|
|
</td>
|
|
}
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
@if (@users.Count != 0)
|
|
{
|
|
<table class="table">
|
|
<thead>
|
|
<tr>
|
|
<th>User</th>
|
|
@foreach (PermissionString permission in permissions)
|
|
{
|
|
<th style="text-align: center;">@permission.PermissionName @EntityName</th>
|
|
}
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
@foreach (User user in users)
|
|
{
|
|
string userid = "[" + user.UserId.ToString() + "]";
|
|
<tr>
|
|
<td>@user.DisplayName</td>
|
|
@foreach (PermissionString permission in permissions)
|
|
{
|
|
var p = permission;
|
|
<td style="text-align: center;">
|
|
<TriStateCheckBox Value=@GetPermissionValue(p.Permissions, userid) Disabled=@GetPermissionDisabled(userid) OnChange="@(e => PermissionChanged(e, p.PermissionName, userid))" />
|
|
</td>
|
|
}
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
}
|
|
<table class="table">
|
|
<tbody>
|
|
<tr>
|
|
<td style="text-align: right;"><label for="Username" class="control-label">User: </label></td>
|
|
<td><input type="text" name="Username" class="form-control" placeholder="Enter Username" @bind="@username" /></td>
|
|
<td style="text-align: left;"><button type="button" class="btn btn-primary" @onclick="@AddUser">Add</button></td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
<br />
|
|
<ModuleMessage Type="MessageType.Error" Message="@message" />
|
|
}
|
|
|
|
@code {
|
|
[Parameter]
|
|
public string EntityName { get; set; }
|
|
|
|
[Parameter]
|
|
public string Permissions { get; set; }
|
|
|
|
string permissionnames = "";
|
|
List<Role> roles;
|
|
List<PermissionString> permissions = new List<PermissionString>();
|
|
List<User> users = new List<User>();
|
|
string username = "";
|
|
string message = "";
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
permissionnames = PageState.ModuleDefinitions.Find(item => item.ModuleDefinitionName == ModuleState.ModuleDefinitionName).Permissions;
|
|
if (string.IsNullOrEmpty(permissionnames))
|
|
{
|
|
permissionnames = "View,Edit";
|
|
}
|
|
roles = await RoleService.GetRolesAsync(ModuleState.SiteId);
|
|
roles.Insert(0, new Role { Name = Constants.AllUsersRole });
|
|
|
|
foreach (string permissionname in permissionnames.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
|
|
{
|
|
permissions.Add(new PermissionString { PermissionName = permissionname, Permissions = "" });
|
|
}
|
|
foreach (PermissionString permissionstring in UserSecurity.GetPermissionStrings(Permissions))
|
|
{
|
|
if (permissions.Find(item => item.PermissionName == permissionstring.PermissionName) != null)
|
|
{
|
|
permissions[permissions.FindIndex(item => item.PermissionName == permissionstring.PermissionName)].Permissions = permissionstring.Permissions;
|
|
}
|
|
if (permissionstring.Permissions.Contains("["))
|
|
{
|
|
foreach (string user in permissionstring.Permissions.Split(new char[] { '[' }, StringSplitOptions.RemoveEmptyEntries))
|
|
{
|
|
if (user.Contains("]"))
|
|
{
|
|
int userid = int.Parse(user.Substring(0, user.IndexOf("]")));
|
|
if (users.Where(item => item.UserId == userid).FirstOrDefault() == null)
|
|
{
|
|
users.Add(await UserService.GetUserAsync(userid, ModuleState.SiteId));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private bool? GetPermissionValue(string Permissions, string SecurityKey)
|
|
{
|
|
if ((";" + Permissions + ";").Contains(";" + "!" + SecurityKey + ";"))
|
|
{
|
|
return false; // deny permission
|
|
}
|
|
else
|
|
{
|
|
if ((";" + Permissions + ";").Contains(";" + SecurityKey + ";"))
|
|
{
|
|
return true; // grant permission
|
|
}
|
|
else
|
|
{
|
|
return null; // not specified
|
|
}
|
|
}
|
|
}
|
|
|
|
private bool GetPermissionDisabled(string RoleName)
|
|
{
|
|
if (RoleName == Constants.AdminRole)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private async Task AddUser()
|
|
{
|
|
if (users.Where(item => item.Username == username).FirstOrDefault() == null)
|
|
{
|
|
try
|
|
{
|
|
User user = await UserService.GetUserAsync(username, ModuleState.SiteId);
|
|
if (user != null)
|
|
{
|
|
users.Add(user);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
message = "Username Does Not Exist";
|
|
}
|
|
}
|
|
username = "";
|
|
}
|
|
|
|
private void PermissionChanged(bool? Value, string PermissionName, string SecurityId)
|
|
{
|
|
bool? selected = Value;
|
|
PermissionString permission = permissions.Find(item => item.PermissionName == PermissionName);
|
|
if (permission != null)
|
|
{
|
|
List<string> ids = permission.Permissions.Split(';').ToList();
|
|
|
|
ids.Remove(SecurityId); // remove grant permission
|
|
ids.Remove("!" + SecurityId); // remove deny permission
|
|
|
|
switch (selected)
|
|
{
|
|
case true:
|
|
ids.Add(SecurityId); // add grant permission
|
|
break;
|
|
case false:
|
|
ids.Add("!" + SecurityId); // add deny permission
|
|
break;
|
|
case null:
|
|
break; // permission not specified
|
|
}
|
|
permissions[permissions.FindIndex(item => item.PermissionName == PermissionName)].Permissions = string.Join(";", ids.ToArray());
|
|
}
|
|
}
|
|
|
|
public string GetPermissions()
|
|
{
|
|
return UserSecurity.SetPermissionStrings(permissions);
|
|
}
|
|
}
|