|
|
using Application.Domain.Entities;
|
|
|
using Application.Models;
|
|
|
using Infrastructure.Application;
|
|
|
using Infrastructure.Data;
|
|
|
using Infrastructure.Extensions;
|
|
|
using Infrastructure.Security;
|
|
|
using Infrastructure.Web.Mvc;
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
using System;
|
|
|
using System.Linq;
|
|
|
|
|
|
namespace UserCenter.Areas.Admin.Controllers
|
|
|
{
|
|
|
[Authorize]
|
|
|
[Area(nameof(Admin))]
|
|
|
public class UserController : CrudController<User, PagedListModel<EditUserModel>, EditUserModel, EditUserModel>
|
|
|
{
|
|
|
private readonly IEncryptionService _encrypitonService;
|
|
|
private readonly AjaxController _ajax;
|
|
|
|
|
|
public UserController(IRepository<User> userRepo, IEncryptionService encrypitonService, AjaxController ajax
|
|
|
) : base(userRepo)
|
|
|
{
|
|
|
this._encrypitonService = encrypitonService;
|
|
|
this._ajax = ajax;
|
|
|
}
|
|
|
|
|
|
public override IQueryable<User> Include(IQueryable<User> query)
|
|
|
{
|
|
|
return query.Include(o => o.UserRoles).ThenInclude(o => o.Role);
|
|
|
}
|
|
|
|
|
|
public override void ToDisplayModel(User entity, EditUserModel model)
|
|
|
{
|
|
|
model.Roles = entity.UserRoles.Select(o => o.RoleId).ToList();
|
|
|
entity.UserRoles.ForEach(o => ViewData.Add(o.RoleId, o.Role.Name));
|
|
|
}
|
|
|
|
|
|
public override void ToEditModel(User entity, EditUserModel model)
|
|
|
{
|
|
|
if (entity != null)
|
|
|
{
|
|
|
model.Roles = entity.UserRoles.Select(o => o.RoleId).ToList();
|
|
|
}
|
|
|
this.ViewData.MultiSelectList(o => model.Roles, () => this._ajax.GetRoleMultiSelectList(model.Roles));
|
|
|
}
|
|
|
|
|
|
public override void ToEntity(EditUserModel model, User entity)
|
|
|
{
|
|
|
if (!string.IsNullOrEmpty(model.Password))
|
|
|
{
|
|
|
entity.PasswordHash = this._encrypitonService.CreatePasswordHash(model.Password, entity.SecurityStamp);
|
|
|
entity.PasswordConfirmed = true;
|
|
|
}
|
|
|
if (string.IsNullOrEmpty(model.Email))
|
|
|
{
|
|
|
entity.EmailConfirmed = true;
|
|
|
}
|
|
|
if (string.IsNullOrEmpty(model.PhoneNumber))
|
|
|
{
|
|
|
entity.PhoneNumberConfirmed = true;
|
|
|
}
|
|
|
foreach (var id in entity.UserRoles.Select(o => o.RoleId).ToList())
|
|
|
{
|
|
|
if (!model.Roles.Any(o => o == id))
|
|
|
{
|
|
|
entity.UserRoles.RemoveAll(o => o.RoleId == id);
|
|
|
}
|
|
|
}
|
|
|
foreach (var id in model.Roles)
|
|
|
{
|
|
|
if (!entity.UserRoles.Any(o => o.RoleId == id))
|
|
|
{
|
|
|
entity.UserRoles.Add(new UserRole { RoleId = id });
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public override IActionResult Add(EditUserModel model)
|
|
|
{
|
|
|
if (string.IsNullOrEmpty(model.Password))
|
|
|
{
|
|
|
ModelState.AddModelError("Password", "<22><><EFBFBD>벻<EFBFBD><EBB2BB>Ϊ<EFBFBD><CEAA>");
|
|
|
}
|
|
|
return base.Add(model);
|
|
|
}
|
|
|
|
|
|
public override IActionResult Edit(EditUserModel model)
|
|
|
{
|
|
|
var entity = this.Repo.Table().FirstOrDefault(o => o.Id == model.Id);
|
|
|
if (entity.UserName != model.UserName)
|
|
|
{
|
|
|
ModelState.AddModelError("UserName", "<22>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
|
|
}
|
|
|
return base.Edit(model);
|
|
|
}
|
|
|
}
|
|
|
} |