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.
iot/projects/Platform/Areas/UserCenter/Controllers/UserController.cs

117 lines
4.2 KiB

using Infrastructure.Application;
using Infrastructure.Data;
using Infrastructure.Extensions;
using Infrastructure.Security;
using Infrastructure.Web.Mvc;
using IoT.Shared.Application.Domain.Entities;
using IoT.Shared.Application.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Platform.Areas.IoTCenter.Controllers;
using System.Linq;
namespace IoT.Shared.Areas.UserCenter.Controllers
{
[Authorize]
[ApiController]
[Route("UserCenter/[controller]/[action]")]
[Area("UserCenter")]
[ControllerScope(ControllerScopeType.Platform)]
public class UserController : CrudController<User, EditUserModel>
{
private readonly IEncryptionService _encrypitonService;
private readonly AjaxController _ajax;
public UserController(IEncryptionService encrypitonService,
AjaxController ajax,
IRepository<User> userRepo) : base(userRepo)
{
this._encrypitonService = encrypitonService;
this._ajax = ajax;
}
public override IQueryable<User> Include(IQueryable<User> query)
{
return query
.Include(o => o.OrganUsers).ThenInclude(o => o.Organ)
.Include(o => o.UserRoles).ThenInclude(o => o.Role);
}
public override IQueryable<User> Query(PagedListModel<EditUserModel> model, IQueryable<User> query)
{
return base.Query(model, query)
.WhereIf(!string.IsNullOrEmpty(model.Query.UserName), o => o.UserName.Contains(model.Query.UserName))
.OrderBy(o => o.UserName);
}
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));
model.Organs = entity.OrganUsers.Select(o => o.OrganId).ToList();
entity.OrganUsers.ForEach(o => ViewData.Add(o.OrganId, o.Organ.Name));
}
public override void ToEditModel(User entity, EditUserModel model)
{
if (entity != null)
{
model.Roles = entity.UserRoles.Select(o => o.RoleId).ToList();
model.Organs = entity.OrganUsers.Select(o => o.OrganId).ToList();
}
this.ViewData.MultiSelectList(o => model.Roles, () => this._ajax.GetRoleMultiSelectList(model.Roles));
this.ViewData.MultiSelectList(o => model.Organs, () => this._ajax.GetOrganMultiSelectList(model.Organs));
}
public override void ToEntity(EditUserModel model, User entity)
{
//password
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;
}
//role
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 });
}
}
//organ
foreach (var id in entity.OrganUsers.Select(o => o.OrganId).ToList())
{
if (!model.Organs.Any(o => o == id))
{
entity.OrganUsers.RemoveAll(o => o.OrganId == id);
}
}
foreach (var id in model.Organs)
{
if (!entity.OrganUsers.Any(o => o.OrganId == id))
{
entity.OrganUsers.Add(new OrganUser { OrganId = id });
}
}
}
}
}