10000 clean up user data by nhathoang989 · Pull Request #500 · mixcore/mix.core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

clean up user data #500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 21 additions & 29 deletions src/Mix.Cms.Api/Controllers/v1/ApiAccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,11 @@ public async Task<ActionResult<RepositoryResponse<AccessTokenViewModel>>> Regist
var createResult = await _userManager.CreateAsync(user, password: model.Password).ConfigureAwait(false);
if (createResult.Succeeded)
{
// Save Additional Data
var formData = await Mix.Cms.Lib.ViewModels.MixDatabaseDatas.Helper.GetFormDataAsync(
MixDatabaseNames.SYSTEM_USER_DATA, MixService.GetAppSetting<string>("DefaultCulture"));
if (formData != null)
{
formData.ParentId = user.UserName;
formData.ParentType = MixDatabaseParentType.User;
formData.Obj = model.UserData;
var saveData = await formData.SaveModelAsync(true);
result.IsSucceed = saveData.IsSucceed;
result.Errors = saveData.Errors;
result.Exception = saveData.Exception;
}
var saveData = await Mix.Cms.Lib.ViewModels.MixDatabaseDatas.Helper.SaveObjAsync(
MixDatabaseNames.SYSTEM_USER_DATA, model.UserData, user.UserName, MixDatabaseParentType.User);
result.IsSucceed = saveData.IsSucceed;
result.Errors = saveData.Errors;
result.Exception = saveData.Exception;

_logger.LogInformation("User created a new account with password.");
user = await _userManager.FindByNameAsync(model.Username).ConfigureAwait(false);
Expand Down Expand Up @@ -274,16 +266,16 @@ public async Task<ActionResult> Details(string viewType, string id = null)
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[HttpGet]
[Route("my-profile")]
public async Task<ActionResult<MixUserViewModel>> MyProfile()
public async Task<ActionResult<MixPortalUserViewModel>> MyProfile()
{
string id = User.Claims.SingleOrDefault(c => c.Type == "Id")?.Value;
ApplicationUser user = await _userManager.FindByIdAsync(id); ;

if (user != null)
{
var mixUser = new MixUserViewModel(user);
var mixUser = new MixPortalUserViewModel(user);
await mixUser.LoadUserDataAsync();
return Ok(new RepositoryResponse<MixUserViewModel>()
return Ok(new RepositoryResponse<MixPortalUserViewModel>()
{
IsSucceed = true,
Data = mixUser
Expand All @@ -297,10 +289,10 @@ public async Task<ActionResult<MixUserViewModel>> MyProfile()
[MixAuthorize]
[HttpPost]
[Route("save")]
public async Task<RepositoryResponse<MixUserViewModel>> Save(
[FromBody] MixUserViewModel model)
public async Task<RepositoryResponse<MixPortalUserViewModel>> Save(
[FromBody] MixPortalUserViewModel model)
{
var result = new RepositoryResponse<MixUserViewModel>() { IsSucceed = true };
var result = new RepositoryResponse<MixPortalUserViewModel>() { IsSucceed = true };
if (model != null && model.User != null)
{
var user = await _userManager.FindByIdAsync(model.User.Id);
Expand Down Expand Up @@ -350,32 +342,32 @@ public async Task<RepositoryResponse<MixUserViewModel>> SaveMyProfile(
string id = User.Claims.SingleOrDefault(c => c.Type == "Id")?.Value;


if (model != null && model.User != null)
if (model != null)
{
if (id != model.User.Id)
if (id != model.Id)
{
result.IsSucceed = false;
result.Errors.Add("Invalid request");
return result;
}

var user = await _userManager.FindByIdAsync(model.User.Id);
user.Email = model.User.Email;
user.FirstName = model.User.FirstName;
user.LastName = model.User.LastName;
user.Avatar = model.User.Avatar;
var user = await _userManager.FindByIdAsync(model.Id);
user.Email = model.Email;
user.FirstName = model.FirstName;
user.LastName = model.LastName;
var updInfo = await _userManager.UpdateAsync(user);
result.IsSucceed = updInfo.Succeeded;
if (result.IsSucceed)
{
var saveData = await model.UserData.SaveModelAsync(true);
var saveData = await Mix.Cms.Lib.ViewModels.MixDatabaseDatas.Helper.SaveObjAsync(
MixDatabaseNames.SYSTEM_USER_DATA, model.UserData, user.UserName, MixDatabaseParentType.User);
result.IsSucceed = saveData.IsSucceed;
result.Errors = saveData.Errors;
result.Exception = saveData.Exception;
}
if (result.IsSucceed && model.IsChangePassword)
{
var changePwd = await _userManager.ChangePasswordAsync(model.User, model.ChangePassword.CurrentPassword, model.ChangePassword.NewPassword);
var changePwd = await _userManager.ChangePasswordAsync(user, model.ChangePassword.CurrentPassword, model.ChangePassword.NewPassword);
if (!changePwd.Succeeded)
{
foreach (var err in changePwd.Errors)
Expand All @@ -390,7 +382,7 @@ public async Task<RepositoryResponse<MixUserViewModel>> SaveMyProfile(
await RefreshTokenViewModel.Repository.RemoveModelAsync(r => r.Id != refreshToken);
}
}
MixFileRepository.Instance.EmptyFolder($"{MixFolders.MixCacheFolder}/Mix/Cms/Lib/ViewModels/Account/MixUsers/_{model.User.Id}");
MixFileRepository.Instance.EmptyFolder($"{MixFolders.MixCacheFolder}/Mix/Cms/Lib/ViewModels/Account/MixUsers/_{model.Id}");
return result;
}
return result;
Expand Down
4 changes: 2 additions & 2 deletions src/Mix.Cms.Lib/ViewModels/Account/MixAccountHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ namespace Mix.Cms.Lib.ViewModels.Account
{
public class MixAccountHelper
{
public static async Task<MixDatabaseDatas.AdditionalViewModel> LoadUserInfoAsync(string userName,
public static async Task<MixDatabaseDatas.AdditionalViewModel> LoadUserInfoAsync(string username,
MixCmsContext _context = null, IDbContextTransaction _transaction = null)
{
var culture = MixService.GetAppSetting<string>(MixAppSettingKeywords.DefaultCulture);
UnitOfWorkHelper<MixCmsContext>.InitTransaction(_context, _transaction, out MixCmsContext context, out IDbContextTransaction transaction, out bool isRoot);
try
{
var getInfo = await MixDatabaseDatas.Helper.LoadAdditionalDataAsync(MixDatabaseParentType.User, userName, MixDatabaseNames.SYSTEM_USER_DATA
var getInfo = await MixDatabaseDatas.Helper.LoadAdditionalDataAsync(MixDatabaseParentType.User, username, MixDatabaseNames.SYSTEM_USER_DATA
, culture, context, transaction);
return getInfo.Data;
}
Expand Down
51 changes: 51 additions & 0 deletions src/Mix.Cms.Lib/ViewModels/Account/MixPortalUserViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Mix.Heart.Models;
using Mix.Identity.Models;
using Mix.Identity.Models.AccountViewModels;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Mix.Cms.Lib.ViewModels.Account
{
public class MixPortalUserViewModel
{
[JsonProperty("user")]
public ApplicationUser User { get; set; }

[JsonProperty("mediaFile")]
public FileViewModel MediaFile { get; set; } = new FileViewModel();

[JsonProperty("userData")]
public MixDatabaseDatas.AdditionalViewModel UserData { get; set; }

[JsonProperty("userRoles")]
public List<NavUserRoleViewModel> UserRoles { get; set; }

#region Change Password

[JsonProperty("resetPassword")]
public ResetPasswordViewModel ResetPassword { get; set; }

[JsonProperty("isChangePassword")]
public bool IsChangePassword { get; set; }

[JsonProperty("changePassword")]
public ChangePasswordViewModel ChangePassword { get; set; }

#endregion Change Password

public MixPortalUserViewModel(ApplicationUser user)
{
User = user;
}

public async Task LoadUserDataAsync()
{
if (User != null)
{
UserData ??= await MixAccountHelper.LoadUserInfoAsync(User.UserName);
UserRoles = MixAccountHelper.GetRoleNavs(User.Id);
}
}
}
}
37 changes: 27 additions & 10 deletions src/Mix.Cms.Lib/ViewModels/Account/MixUserViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@
using Mix.Identity.Models;
using Mix.Identity.Models.AccountViewModels;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Mix.Cms.Lib.ViewModels.Account
{
public class MixUserViewModel
{
[JsonProperty("user")]
public ApplicationUser User { get; set; }

[JsonProperty("mediaFile")]
public FileViewModel MediaFile { get; set; } = new FileViewModel();
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("username")]
public string Username { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("firstName")]
public string FirstName { get; set; }
[JsonProperty("lastName")]
public string LastName { get; set; }

[JsonProperty("userData")]
public MixDatabaseDatas.AdditionalViewModel UserData { get; set; }
public JObject UserData { get; set; }

[JsonProperty("userRoles")]
public List<NavUserRoleViewModel> UserRoles { get; set; }
Expand All @@ -36,15 +42,26 @@ public class MixUserViewModel

public MixUserViewModel(ApplicationUser user)
{
User = user;
if (user != null)
{
Id = user.Id;
Username = user.UserName;
Email = user.Email;
FirstName = user.FirstName;
LastName = user.LastName;
}
}

public async Task LoadUserDataAsync()
{
if (User != null)
if (!string.IsNullOrEmpty(Username))
{
UserData ??= await MixAccountHelper.LoadUserInfoAsync(User.UserName);
UserRoles = MixAccountHelper.GetRoleNavs(User.Id);
if(UserData == null)
{
var data = await MixAccountHelper.LoadUserInfoAsync(Username);
UserData = data.Obj;
}
UserRoles = MixAccountHelper.GetRoleNavs(Id);
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions src/Mix.Cms.Lib/ViewModels/MixDatabaseDatas/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,35 @@ public static async Task<RepositoryResponse<AdditionalViewModel>> LoadAdditional
}
}

public static async Task<RepositoryResponse<FormViewModel>> SaveObjAsync(string databaseName, JObject obj, string parentId = null, MixDatabaseParentType? parentType = null, string culture = null)
{
culture ??= MixService.GetAppSetting<string>("DefaultCulture");
string id = obj.Value<string>("id");
FormViewModel formData;

if (id == null)
{
formData = await GetFormDataAsync(databaseName, culture);
}
else
{
var getFormData = await FormViewModel.Repository.GetSingleModelAsync(m => m.Id == id && m.Specificulture == culture);
formData = getFormData.Data;
}

if (formData != null)
{
formData.ParentId = parentId;
if (parentType.HasValue)
{
formData.ParentType = parentType.Value;
}
formData.Obj = obj;
return await formData.SaveModelAsync(true);
}
return new();
}

public static async Task<FormViewModel> GetFormDataAsync(string mixDatabase, string culture)
{
_ = int.TryParse(mixDatabase, out int mixDatabaseId);
Expand Down
4 changes: 2 additions & 2 deletions src/Mix.Cms.Lib/ViewModels/MixThemes/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ public static async Task<RepositoryResponse<string>> ExportTheme(
}
}

public static async Task<RepositoryResponse<InitViewModel>> InitTheme(string model, string userName, string culture, IFormFile assets, IFormFile theme)
public static async Task<RepositoryResponse<InitViewModel>> InitTheme(string model, string username, string culture, IFormFile assets, IFormFile theme)
{
var json = JObject.Parse(model);
var data = json.ToObject<InitViewModel>();
if (data != null)
{
data.CreatedBy = userName;
data.CreatedBy = username;
data.Status = MixContentStatus.Published;
string importFolder = MixFolders.ThemePackage;
if (theme != null)
Expand Down
30 changes: 22 additions & 8 deletions src/Mix.Cms.Web/wwwroot/mix-app/js/app-client.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -2348,7 +2348,7 @@ app.controller("UserController", [
"UserService",
function ($rootScope, $scope, authService, userService) {
$scope.loginData = {
userName: "",
username: "",
password: "",
rememberme: true,
};
Expand All @@ -2363,8 +2363,9 @@ app.controller("UserController", [
$scope.init = function () {
authService.fillAuthData().then((resp) => {
if (authService.authentication.info) {
$scope.user = authService.authentication.info;
$scope.showLogin = false;
$scope.userData = authService.authentication.info.userData.obj;
$scope.userData = authService.authentication.info.userData;
} else {
$scope.showLogin = true;
}
Expand All @@ -2383,12 +2384,25 @@ app.controller("UserController", [
}
};

$scope.register = async function () {
$scope.save = async function () {
$rootScope.isBusy = true;
var resp = await userService.register($scope.user);
var resp = null;
if (!user.id) {
resp = await userService.register($scope.user);
} else {
resp = await userService.saveUser($scope.user);
}
if (resp && resp.isSucceed) {
$scope.activedUser = resp.data;
$scope.user = resp.data;
$rootScope.showMessage("Update successfully!", "success");
authService
.refreshToken(
authService.authentication.refresh_token,
authService.authentication.access_token
)
.then(() => {
window.location = window.location;
});
$rootScope.isBusy = false;
$scope.$apply();
} else {
Expand All @@ -2404,7 +2418,7 @@ app.controller("UserController", [
$rootScope.isBusy = true;
var response = await userService.getMyProfile();
if (response.isSucceed) {
$scope.activedUser = response.data;
$scope.user = response.data;
$rootScope.isBusy = false;
$scope.$apply();
} else {
Expand All @@ -2415,10 +2429,10 @@ app.controller("UserController", [
};
$scope.save = async function () {
$rootScope.isBusy = true;
var resp = await userService.saveUser($scope.activedUser);
var resp = await userService.saveUser($scope.user);
if (resp && resp.isSucceed) {
$rootScope.showMessage("Update successfully!", "success");
if ($scope.activedUser.id == authService.authentication.id) {
if ($scope.user.id == authService.authentication.info.id) {
authService
.refreshToken(
authService.authentication.refresh_token,
Expand Down
Loading
0