You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.9 KiB
89 lines
2.9 KiB
using System.Linq;
|
|
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 UserCenter.Services;
|
|
|
|
namespace UserCenter.Areas.Admin.Controllers
|
|
{
|
|
[Authorize]
|
|
[Area(nameof(Admin))]
|
|
public class UserController : CrudController<User, PagedListModel<EditUserModel>, EditUserModel, EditUserModel>
|
|
{
|
|
private readonly IRepository<User> _userRepo;
|
|
private readonly IEncryptionService _encrypitonService;
|
|
private readonly AjaxController _ajax;
|
|
private readonly FaceRecognitionService _frs;
|
|
|
|
public UserController(IRepository<User> userRepo, IEncryptionService encrypitonService, AjaxController ajax, FaceRecognitionService frs) : base(userRepo)
|
|
{
|
|
this._userRepo = userRepo;
|
|
this._encrypitonService = encrypitonService;
|
|
this._ajax = ajax;
|
|
this._frs = frs;
|
|
}
|
|
|
|
public override IQueryable<User> Include(IQueryable<User> query)
|
|
{
|
|
return query.Include(o => o.UserRoles).ThenInclude(o => o.Role);
|
|
}
|
|
|
|
public override void ToModel(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);
|
|
}
|
|
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 void OnEdit(User entity, EditUserModel model)
|
|
{
|
|
model.UserName = entity.UserName;
|
|
}
|
|
|
|
public override void OnAdded(User entity)
|
|
{
|
|
this._frs.AddFace(entity.UserName, entity.FaceImage);
|
|
}
|
|
|
|
public override void OnUpdeted(User entity)
|
|
{
|
|
this._frs.UpdateFace(entity.UserName, entity.FaceImage);
|
|
}
|
|
|
|
public override void OnDeleted(User entity)
|
|
{
|
|
this._frs.RemoveFace(entity.UserName);
|
|
}
|
|
}
|
|
} |