-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathProfile.razor
More file actions
320 lines (287 loc) · 15.4 KB
/
Profile.razor
File metadata and controls
320 lines (287 loc) · 15.4 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
@page "/user/profile"
@using CleanArchitecture.Blazor.Application.Features.Fusion
@using ResizeMode = SixLabors.ImageSharp.Processing.ResizeMode
@using Size = SixLabors.ImageSharp.Size
@using SixLabors.ImageSharp.Processing
@using SixLabors.ImageSharp
@using SixLabors.ImageSharp.Formats.Png
@using CleanArchitecture.Blazor.Domain.Identity
@using CleanArchitecture.Blazor.Application.Common.Interfaces.Identity
@using CleanArchitecture.Blazor.Server.UI.Services.JsInterop
@using CleanArchitecture.Blazor.Domain.Common.Enums
@inherits OwningComponentBase
@inject IStringLocalizer<Profile> L
@inject IUploadService UploadService
@inject IOnlineUserTracker OnlineUserTracker
@inject UserProfileStateService UserProfileStateService
<PageTitle>@Title</PageTitle>
@if (model is null)
{
<MudProgressLinear Color="Color.Primary" Indeterminate="true" Class="my-7" />
}
else
{
<MudTabs Outlined="true" Position="Position.Top" Rounded="true" Border="true" Elevation="6" ActivePanelIndexChanged="ActivePanelIndexChanged"
ApplyEffectsToContainer="true" Class="mt-8" PanelClass="pa-6">
<MudTabPanel Text="@L["Profile"]">
<MudForm Model="@model" @ref="@_form" Validation="@(Validator.ValidateValue(model))" Style="display: flex; align-content: center; align-items: center; flex-direction: column;">
<MudGrid Justify="Justify.Center" Spacing="2" Style="max-width:600px;display:flex;">
<MudItem sm="12" xs="12">
<div class="d-flex justify-center">
<MudElement Class="d-flex flex-column align-center">
@if (string.IsNullOrEmpty(model.ProfilePictureDataUrl))
{
<MudAvatar Style="height:120px; width:120px; font-size:2rem;">@(string.IsNullOrEmpty(model.UserName) ? "" : model.UserName.ToUpper().First())</MudAvatar>
}
else
{
<MudAvatar Style="height:120px; width:120px; font-size:2rem;">
<MudImage Src="@model.ProfilePictureDataUrl"></MudImage>
</MudAvatar>
}
@if (model.AssignedRoles is not null)
{
<div class="d-flex">
@foreach (var role in model.AssignedRoles)
{
<MudChip T="string" Size="MudBlazor.Size.Small">@role</MudChip>
}
</div>
}
</MudElement>
<MudTooltip Text="@L["Click upload a image"]">
<MudFileUpload T="IBrowserFile" Accept=".jpg, .jpeg, .png, .webp" FilesChanged="UploadPhoto" Style="margin-top:-10px;margin-left:-15px">
<CustomContent>
<MudIconButton Icon="@Icons.Material.Filled.PhotoCamera">
</MudIconButton>
</CustomContent>
<SelectedTemplate />
</MudFileUpload>
</MudTooltip>
</div>
</MudItem>
<MudItem sm="6" xs="12">
<MudTextField For="@(() => model.TenantName)" @bind-Value="model.TenantName" Label="@L["Tenant Name"]" ReadOnly="true"></MudTextField>
</MudItem>
<MudItem xs="12" sm="6">
<MudTextField For="@(() => model.SuperiorName)" @bind-Value="model.SuperiorName" Label="@L["Superior Name"]" ReadOnly="true"></MudTextField>
</MudItem>
<MudItem sm="6" xs="12">
<MudTextField For="@(() => model.UserName)" @bind-Value="model.UserName" Label="@L["User Name"]" ReadOnly="true"></MudTextField>
</MudItem>
<MudItem sm="6" xs="12">
<MudTextField For="@(() => model.Email)" @bind-Value="model.Email" Label="@L["Email"]" ReadOnly="true"></MudTextField>
</MudItem>
<MudItem sm="6" xs="12">
<MudTextField For="@(() => model.DisplayName)" @bind-Value="model.DisplayName" Label="@L["Full Name"]" ></MudTextField>
</MudItem>
<MudItem sm="6" xs="12">
<MudTextField For="@(() => model.PhoneNumber)" @bind-Value="model.PhoneNumber" Label="@L["Phone Number"]" ></MudTextField>
</MudItem>
<MudItem sm="6" xs="12">
<TimeZoneAutocomplete T="string" For="@(() => model.TimeZoneId)" @bind-Value="model.TimeZoneId" Label="@L["Time Zone"]" ></TimeZoneAutocomplete>
</MudItem>
<MudItem sm="6" xs="12">
<LanguageAutocomplete T="string" For="@(() => model.LanguageCode)" @bind-Value="model.LanguageCode" Label="@L["Language"]" ></LanguageAutocomplete>
</MudItem>
<MudItem sm="12" xs="12" Class="d-flex justify-end">
<MudButton ButtonType="ButtonType.Button" Disabled="@_submitting" Color="Color.Primary" Class="ml-auto" OnClick="@(async () => await Submit())">
@if (_submitting)
{
<MudProgressCircular Class="ms-n1" Size="MudBlazor.Size.Small" Indeterminate="true" />
<MudText Class="ms-2">@ConstantString.Waiting </MudText>
}
else
{
<MudText>@ConstantString.Save</MudText>
}
</MudButton>
</MudItem>
</MudGrid>
</MudForm>
</MudTabPanel>
<MudTabPanel Text="@L["Change Password"]">
<MudForm Model="@_changepassword" @ref="@_passwordform" Validation="@(Validator.ValidateValue(_changepassword))" Style="display: flex; align-content: center; align-items: center; flex-direction: column;">
<MudGrid Spacing="2" Justify="Justify.Center" Style="max-width:300px">
<MudItem sm="12" xs="12">
<MudTextField InputType="InputType.Password"
Label="@L["Current Password"]"
For="@(() => _changepassword.CurrentPassword)"
@bind-Value="_changepassword.CurrentPassword"
Required="true" />
</MudItem>
<MudItem xs="12">
<MudTextField InputType="InputType.Password"
Label="@L["New Password"]"
For="@(() => _changepassword.NewPassword)"
@bind-Value="_changepassword.NewPassword"
Required="true" />
</MudItem>
<MudItem xs="12">
<MudTextField InputType="InputType.Password"
Label="@L["Confirm New Password"]"
For="@(() => _changepassword.ConfirmPassword)"
@bind-Value="_changepassword.ConfirmPassword"
Required="true" />
</MudItem>
<MudItem sm="12" xs="12" Class="d-flex justify-end">
<MudButton ButtonType="ButtonType.Button" Color="Color.Primary" Class="ml-auto" OnClick="@(async () => await ChangePassword())">
@if (_submitting)
{
<MudProgressCircular Class="ms-n1" Size="MudBlazor.Size.Small" Indeterminate="true" />
<MudText Class="ms-2">@ConstantString.Waiting </MudText>
}
else
{
<MudText>@L["Change Password"]</MudText>
}
</MudButton>
</MudItem>
</MudGrid>
</MudForm>
</MudTabPanel>
<MudTabPanel Text="@L["Org Chart"]">
<div class="chart-container"
style="height: calc(100vh - 265px);">
</div>
</MudTabPanel>
</MudTabs>
}
@code {
[Inject] protected IServiceProvider Services { get; init; } = null!;
private string _currentUserName = string.Empty;
private UserProfile? UserProfile { get; set; }
private UserManager<ApplicationUser> UserManager = null!;
public string Title { get; set; } = "Profile";
private MudForm? _form;
private MudForm? _passwordform;
private bool _submitting;
private ChangePasswordModel _changepassword { get; } = new();
private readonly List<OrgItem> _orgData = new();
public string Id => Guid.NewGuid().ToString();
private ChangeUserProfileModel model = null!;
[CascadingParameter] private Task<AuthenticationState> AuthState { get; set; } = default!;
private async void ActivePanelIndexChanged(int index)
{
if (index == 2)
{
await LoadOrgData();
}
}
private async Task LoadOrgData()
{
var list = await UserManager.Users.Include(x => x.UserRoles).ThenInclude(x => x.Role).Include(x => x.Superior).ToListAsync();
foreach (var item in list)
{
var roles = await UserManager.GetRolesAsync(item);
var count = await UserManager.Users.Where(x => x.SuperiorId == item.Id).CountAsync();
var orgitem = new OrgItem();
orgitem.Id = item.Id;
orgitem.Name = item.DisplayName ?? item.UserName;
orgitem.Area = item.Tenant?.Name;
orgitem.ProfileUrl = item.ProfilePictureDataUrl;
orgitem.ImageUrl = item.ProfilePictureDataUrl;
if (_currentUserName == item.UserName)
orgitem.IsLoggedUser = true;
orgitem.Size = "";
orgitem.Tags = item.PhoneNumber ?? item.Email;
if (roles != null && roles.Count > 0)
orgitem.PositionName = string.Join(',', roles);
orgitem.ParentId = item.SuperiorId;
orgitem.DirectSubordinates = count;
_orgData.Add(orgitem);
}
await new OrgChart(JS).Create(_orgData);
}
protected override async Task OnInitializedAsync()
{
UserManager = Services.GetRequiredService<UserManager<ApplicationUser>>();
var state = await AuthState;
_currentUserName = state.User.Identity?.Name??string.Empty;
await UserProfileStateService.InitializeAsync(state.User.Identity?.Name??string.Empty);
model = UserProfileMapper.ToChangeUserProfileModel(UserProfileStateService.UserProfile);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
private async Task UploadPhoto(IBrowserFile file)
{
var filestream = file.OpenReadStream(GlobalVariable.MaxAllowedSize);
var imgstream = new MemoryStream();
await filestream.CopyToAsync(imgstream);
imgstream.Position = 0;
using (var outStream = new MemoryStream())
{
using (var image = Image.Load(imgstream))
{
var user = await UserManager.FindByNameAsync(model.UserName) ?? throw new NotFoundException($"The application user [{model.UserName}] was not found.");
image.Mutate(i => i.Resize(new ResizeOptions { Mode = ResizeMode.Crop, Size = new Size(128, 128) }));
image.Save(outStream, PngFormat.Instance);
var result = await UploadService.UploadAsync(new UploadRequest($"{user.Id}_{DateTime.UtcNow.Ticks}.png", UploadType.ProfilePicture, outStream.ToArray(), true));
model.ProfilePictureDataUrl = result;
user.ProfilePictureDataUrl = model.ProfilePictureDataUrl;
await UserManager.UpdateAsync(user);
Snackbar.Add(L["The avatar has been updated"], Severity.Info);
UserProfileStateService.UpdateUserProfile(user.UserName!,user.ProfilePictureDataUrl, user.DisplayName, user.PhoneNumber,user.TimeZoneId, user.LanguageCode);
await OnlineUserTracker.Update(user.Id,
user.UserName??"",
user.DisplayName ?? "",
user.ProfilePictureDataUrl);
}
}
}
private async Task Submit()
{
_submitting = true;
try
{
await _form!.Validate();
if (_form.IsValid)
{
var user = await UserManager.FindByNameAsync(_currentUserName) ?? throw new NotFoundException($"The application user [{_currentUserName}] was not found.");
user.PhoneNumber = model.PhoneNumber;
user.DisplayName = model.DisplayName;
user.TimeZoneId = model.TimeZoneId;
user.LanguageCode = model.LanguageCode;
user.ProfilePictureDataUrl = model.ProfilePictureDataUrl;
await UserManager.UpdateAsync(user);
UserProfileStateService.UpdateUserProfile(user.UserName!,user.ProfilePictureDataUrl, user.DisplayName, user.PhoneNumber,user.TimeZoneId, user.LanguageCode);
await OnlineUserTracker.Update(user.Id,
user.UserName ?? "",
user.DisplayName ?? "",
user.ProfilePictureDataUrl??"");
Snackbar.Add($"{ConstantString.SaveSuccess}", Severity.Info);
}
}
finally
{
_submitting = false;
}
}
private async Task ChangePassword()
{
_submitting = true;
try
{
await _passwordform!.Validate();
if (_passwordform!.IsValid)
{
var user = await UserManager.FindByNameAsync(model.UserName) ?? throw new NotFoundException($"The application user [{model.UserName}] was not found."); ;
var result = await UserManager.ChangePasswordAsync(user, _changepassword.CurrentPassword, _changepassword.NewPassword);
if (result.Succeeded)
{
Snackbar.Add($"{L["Password changed successfully"]}", Severity.Info);
}
else
{
Snackbar.Add($"{string.Join(",", result.Errors.Select(x => x.Description).ToArray())}", Severity.Error);
}
}
}
finally
{
_submitting = false;
}
}
}