forked from fullstackhero/blazor-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUsers.razor.cs
More file actions
127 lines (117 loc) · 4.69 KB
/
Users.razor.cs
File metadata and controls
127 lines (117 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using BlazorHero.CleanArchitecture.Application.Responses.Identity;
using MudBlazor;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using BlazorHero.CleanArchitecture.Shared.Constants.Application;
using BlazorHero.CleanArchitecture.Shared.Constants.Permission;
using Microsoft.AspNetCore.Authorization;
using Microsoft.JSInterop;
using Polly;
namespace BlazorHero.CleanArchitecture.Client.Pages.Identity
{
public partial class Users
{
private List<UserResponse> _userList = new();
private UserResponse _user = new();
private string _searchString = "";
private bool _dense = false;
private bool _striped = true;
private bool _bordered = false;
private ClaimsPrincipal _currentUser;
private bool _canCreateUsers;
private bool _canSearchUsers;
private bool _canExportUsers;
private bool _canViewRoles;
private bool _loaded;
protected override async Task OnInitializedAsync()
{
_currentUser = await _authenticationManager.CurrentUser();
_canCreateUsers = (await _authorizationService.AuthorizeAsync(_currentUser, Permissions.Users.Create)).Succeeded;
_canSearchUsers = (await _authorizationService.AuthorizeAsync(_currentUser, Permissions.Users.Search)).Succeeded;
_canExportUsers = (await _authorizationService.AuthorizeAsync(_currentUser, Permissions.Users.Export)).Succeeded;
_canViewRoles = (await _authorizationService.AuthorizeAsync(_currentUser, Permissions.Roles.View)).Succeeded;
await GetUsersAsync();
_loaded = true;
}
private async Task GetUsersAsync()
{
var response = await _userManager.GetAllAsync();
if (response.Succeeded)
{
_userList = response.Data.ToList();
}
else
{
foreach (var message in response.Messages)
{
_snackBar.Add(message, Severity.Error);
}
}
}
private bool Search(UserResponse user)
{
if (string.IsNullOrWhiteSpace(_searchString)) return true;
if (user.FirstName?.Contains(_searchString, StringComparison.OrdinalIgnoreCase) == true)
{
return true;
}
if (user.LastName?.Contains(_searchString, StringComparison.OrdinalIgnoreCase) == true)
{
return true;
}
if (user.Email?.Contains(_searchString, StringComparison.OrdinalIgnoreCase) == true)
{
return true;
}
if (user.PhoneNumber?.Contains(_searchString, StringComparison.OrdinalIgnoreCase) == true)
{
return true;
}
if (user.UserName?.Contains(_searchString, StringComparison.OrdinalIgnoreCase) == true)
{
return true;
}
return false;
}
private async Task ExportToExcel()
{
var base64 = await _userManager.ExportToExcelAsync(_searchString);
await _jsRuntime.InvokeVoidAsync("Download", new
{
ByteArray = base64,
FileName = $"{nameof(Users).ToLower()}_{DateTime.Now:ddMMyyyyHHmmss}.xlsx",
MimeType = ApplicationConstants.MimeTypes.OpenXml
});
_snackBar.Add(string.IsNullOrWhiteSpace(_searchString)
? _localizer["Users exported"]
: _localizer["Filtered Users exported"], Severity.Success);
}
private async Task InvokeModal()
{
var parameters = new DialogParameters();
var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.Small, FullWidth = true, DisableBackdropClick = true };
var dialog = _dialogService.Show<RegisterUserModal>(_localizer["Register New User"], parameters, options);
var result = await dialog.Result;
if (!result.Cancelled)
{
await GetUsersAsync();
}
}
private void ViewProfile(string userId)
{
_navigationManager.NavigateTo($"/user-profile/{userId}");
}
private void ManageRoles(string userId, string email)
{
if (email == "mukesh@blazorhero.com") _snackBar.Add(_localizer["Not Allowed."], Severity.Error);
else _navigationManager.NavigateTo($"/identity/user-roles/{userId}");
}
private void SendMail(string userId)
{
_userManager.ResendConfirmationMailAsync(userId);
}
}
}