Dynamic User Roles

This commit is contained in:
Shaun Walker
2019-08-20 16:43:35 -04:00
parent de4fa48a29
commit 42c6efbfdb
51 changed files with 942 additions and 193 deletions

View File

@ -0,0 +1,40 @@
@using Oqtane.Services
@using Oqtane.Models
@using Oqtane.Modules
@using Oqtane.Client.Modules.Controls
@inherits ModuleBase
@inject IRoleService RoleService
@if (Roles == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
@foreach (var Role in Roles)
{
<tr>
<td>@Role.Name</td>
</tr>
}
</tbody>
</table>
}
@code {
public override SecurityAccessLevel SecurityAccessLevel { get { return SecurityAccessLevel.Admin; } }
List<Role> Roles;
protected override async Task OnInitializedAsync()
{
Roles = await RoleService.GetRolesAsync(PageState.Site.SiteId);
}
}

View File

@ -30,12 +30,12 @@ else
}
@code {
public override SecurityAccessLevel SecurityAccessLevel { get { return SecurityAccessLevel.Host; } }
public override SecurityAccessLevel SecurityAccessLevel { get { return SecurityAccessLevel.Admin; } }
List<User> Users;
protected override async Task OnInitializedAsync()
{
Users = await UserService.GetUsersAsync();
Users = await UserService.GetUsersAsync(PageState.Site.SiteId);
}
}

View File

@ -0,0 +1,21 @@
using Oqtane.Models;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Oqtane.Services
{
public interface IRoleService
{
Task<List<Role>> GetRolesAsync();
Task<List<Role>> GetRolesAsync(int SiteId);
Task<Role> GetRoleAsync(int RoleId);
Task<Role> AddRoleAsync(Role Role);
Task<Role> UpdateRoleAsync(Role Role);
Task DeleteRoleAsync(int RoleId);
}
}

View File

@ -0,0 +1,16 @@
using Oqtane.Models;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Oqtane.Services
{
public interface IUserRoleService
{
Task<List<UserRole>> GetUserRolesAsync();
Task<List<UserRole>> GetUserRolesAsync(int UserId);
Task<UserRole> GetUserRoleAsync(int UserRoleId);
Task<UserRole> AddUserRoleAsync(UserRole UserRole);
Task<UserRole> UpdateUserRoleAsync(UserRole UserRole);
Task DeleteUserRoleAsync(int UserRoleId);
}
}

View File

@ -6,11 +6,11 @@ namespace Oqtane.Services
{
public interface IUserService
{
Task<List<User>> GetUsersAsync();
Task<List<User>> GetUsersAsync(int SiteId);
Task<User> GetUserAsync(int UserId);
Task<User> GetUserAsync(int UserId, int SiteId);
Task<User> GetUserAsync(string Username);
Task<User> GetUserAsync(string Username, int SiteId);
Task<User> AddUserAsync(User User);
@ -18,8 +18,6 @@ namespace Oqtane.Services
Task DeleteUserAsync(int UserId);
Task<User> GetCurrentUserAsync();
Task<User> LoginUserAsync(User User);
Task LogoutUserAsync();

View File

@ -0,0 +1,60 @@
using Oqtane.Models;
using System.Threading.Tasks;
using System.Net.Http;
using System.Linq;
using Microsoft.AspNetCore.Components;
using System.Collections.Generic;
using Oqtane.Shared;
namespace Oqtane.Services
{
public class RoleService : ServiceBase, IRoleService
{
private readonly HttpClient http;
private readonly SiteState sitestate;
private readonly IUriHelper urihelper;
public RoleService(HttpClient http, SiteState sitestate, IUriHelper urihelper)
{
this.http = http;
this.sitestate = sitestate;
this.urihelper = urihelper;
}
private string apiurl
{
get { return CreateApiUrl(sitestate.Alias, urihelper.GetAbsoluteUri(), "Role"); }
}
public async Task<List<Role>> GetRolesAsync()
{
List<Role> Roles = await http.GetJsonAsync<List<Role>>(apiurl);
return Roles.OrderBy(item => item.Name).ToList();
}
public async Task<List<Role>> GetRolesAsync(int SiteId)
{
List<Role> Roles = await http.GetJsonAsync<List<Role>>(apiurl + "?siteid=" + SiteId.ToString());
return Roles.OrderBy(item => item.Name).ToList();
}
public async Task<Role> GetRoleAsync(int RoleId)
{
return await http.GetJsonAsync<Role>(apiurl + "/" + RoleId.ToString());
}
public async Task<Role> AddRoleAsync(Role Role)
{
return await http.PostJsonAsync<Role>(apiurl, Role);
}
public async Task<Role> UpdateRoleAsync(Role Role)
{
return await http.PutJsonAsync<Role>(apiurl + "/" + Role.SiteId.ToString(), Role);
}
public async Task DeleteRoleAsync(int RoleId)
{
await http.DeleteAsync(apiurl + "/" + RoleId.ToString());
}
}
}

View File

@ -0,0 +1,59 @@
using Oqtane.Models;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Oqtane.Shared;
namespace Oqtane.Services
{
public class UserRoleService : ServiceBase, IUserRoleService
{
private readonly HttpClient http;
private readonly SiteState sitestate;
private readonly IUriHelper urihelper;
public UserRoleService(HttpClient http, SiteState sitestate, IUriHelper urihelper)
{
this.http = http;
this.sitestate = sitestate;
this.urihelper = urihelper;
}
private string apiurl
{
get { return CreateApiUrl(sitestate.Alias, urihelper.GetAbsoluteUri(), "UserRole"); }
}
public async Task<List<UserRole>> GetUserRolesAsync()
{
return await http.GetJsonAsync<List<UserRole>>(apiurl);
}
public async Task<List<UserRole>> GetUserRolesAsync(int UserId)
{
return await http.GetJsonAsync<List<UserRole>>(apiurl + "?userid=" + UserId.ToString());
}
public async Task<UserRole> GetUserRoleAsync(int UserRoleId)
{
return await http.GetJsonAsync<UserRole>(apiurl + "/" + UserRoleId.ToString());
}
public async Task<UserRole> AddUserRoleAsync(UserRole UserRole)
{
return await http.PostJsonAsync<UserRole>(apiurl, UserRole);
}
public async Task<UserRole> UpdateUserRoleAsync(UserRole UserRole)
{
return await http.PutJsonAsync<UserRole>(apiurl + "/" + UserRole.UserRoleId.ToString(), UserRole);
}
public async Task DeleteUserRoleAsync(int UserRoleId)
{
await http.DeleteAsync(apiurl + "/" + UserRoleId.ToString());
}
}
}

View File

@ -27,20 +27,20 @@ namespace Oqtane.Services
get { return CreateApiUrl(sitestate.Alias, urihelper.GetAbsoluteUri(), "User"); }
}
public async Task<List<User>> GetUsersAsync()
public async Task<List<User>> GetUsersAsync(int SiteId)
{
List<User> users = await http.GetJsonAsync<List<User>>(apiurl);
List<User> users = await http.GetJsonAsync<List<User>>(apiurl + "?siteid=" + SiteId.ToString());
return users.OrderBy(item => item.DisplayName).ToList();
}
public async Task<User> GetUserAsync(int UserId)
public async Task<User> GetUserAsync(int UserId, int SiteId)
{
return await http.GetJsonAsync<User>(apiurl + "/" + UserId.ToString());
return await http.GetJsonAsync<User>(apiurl + "/" + UserId.ToString() + "?siteid=" + SiteId.ToString());
}
public async Task<User> GetUserAsync(string Username)
public async Task<User> GetUserAsync(string Username, int SiteId)
{
return await http.GetJsonAsync<User>(apiurl + "/name/" + Username);
return await http.GetJsonAsync<User>(apiurl + "/name/" + Username + "?siteid=" + SiteId.ToString());
}
public async Task<User> AddUserAsync(User User)
@ -57,11 +57,6 @@ namespace Oqtane.Services
await http.DeleteAsync(apiurl + "/" + UserId.ToString());
}
public async Task<User> GetCurrentUserAsync()
{
return await http.GetJsonAsync<User>(apiurl + "/current");
}
public async Task<User> LoginUserAsync(User User)
{
return await http.PostJsonAsync<User>(apiurl + "/login", User);
@ -80,7 +75,7 @@ namespace Oqtane.Services
if (User != null)
{
//super user always has full access
// super user always has full access
isAllowed = User.IsSuperUser;
}

View File

@ -169,7 +169,8 @@
user.Password = HostPassword;
user.IsSuperUser = true;
user.Roles = "";
await UserService.AddUserAsync(user);
user = await UserService.AddUserAsync(user);
UriHelper.NavigateTo("", true);
}
else

View File

@ -21,8 +21,8 @@
}
else
{
// layout does not exist with type specified
}
// layout does not exist with type specified
}
};
}
}

View File

@ -195,7 +195,7 @@
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
if (authState.User.Identity.IsAuthenticated)
{
user = await UserService.GetUserAsync(authState.User.Identity.Name);
user = await UserService.GetUserAsync(authState.User.Identity.Name, site.SiteId);
}
}
else

View File

@ -20,8 +20,8 @@
}
else
{
// theme does not exist with type specified
builder.OpenComponent(0, Type.GetType(Constants.ModuleMessageControl));
// theme does not exist with type specified
builder.OpenComponent(0, Type.GetType(Constants.ModuleMessageControl));
builder.AddAttribute(1, "Type", MessageType.Error);
builder.AddAttribute(2, "Message", "Error Loading Page Theme " + PageState.Page.ThemeType);
builder.CloseComponent();

View File

@ -46,6 +46,8 @@ namespace Oqtane.Client
services.AddScoped<IModuleService, ModuleService>();
services.AddScoped<IPageModuleService, PageModuleService>();
services.AddScoped<IUserService, UserService>();
services.AddScoped<IRoleService, RoleService>();
services.AddScoped<IUserRoleService, UserRoleService>();
services.AddScoped<ISettingService, SettingService>();
// dynamically register module contexts and repository services

View File

@ -1,8 +1,7 @@
@using Oqtane.Themes
@using Oqtane.Services
@using Oqtane.Services
@using Oqtane.Providers
@using Oqtane.Shared
@using Oqtane.Models
@using Microsoft.JSInterop
@inherits ThemeObjectBase
@inject IUriHelper UriHelper
@ -51,8 +50,8 @@
{
// client-side Blazor
authstateprovider.NotifyAuthenticationChanged();
PageState.Reload = Constants.ReloadPage;
UriHelper.NavigateTo(NavigateUrl());
PageState.Reload = Constants.ReloadSite;
UriHelper.NavigateTo(NavigateUrl(PageState.Page.Path));
}
}
}