using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Web.Http; using System.Web.Http.Cors; using System.Web.Http.Description; using FHT.Agency.CommonTypes; using FHT.Agency.Queries.ImplementedInterfaces; using FHT.Agency.Services.ImplementedInterfaces; using FHT.Agency.UI.InputModels; using FHT.Agency.UI.ViewModels; using FHT.Agency.Web.Extensions; using FHT.Agency.Web.Filters; using FHT.Agency.Web.Utility; using FHT.ApiCommon; using FHT.Core.UI.ViewModels; using FHT.DDD; using FHT.Utilities; using Microsoft.AspNet.Identity; namespace FHT.Agency.Web.ApiControllers { [Authorize] [RoutePrefix("api/account")] [EnableCors(origins: "*", headers: "*", methods: "*")] public class AccountApiController : ApiController { private readonly IRoleClaimService _roleClaimService; private readonly IRoleClaimQuery _roleClaimQuery; private readonly IAccountService _accountService; private readonly IUserQuery _userQuery; public AccountApiController(IRoleClaimService roleClaimService, IRoleClaimQuery roleClaimQuery, IAccountService accountService, IUserQuery userQuery) { _roleClaimService = roleClaimService; _roleClaimQuery = roleClaimQuery; _accountService = accountService; _userQuery = userQuery; } #region 查询 [ClaimsAuthorize(FhtUserClaimType.RoleClaimManage)] [HttpGet] [Route("_getAllRoleClaims")] [ResponseType(typeof(List))] public async Task GetAllRoleClaimsAsync() { List roleClaims =await _roleClaimQuery.GetAllRoleClaimsAsync(); return Ok(roleClaims); } [ClaimsAuthorize(FhtUserClaimType.RoleClaimManage)] [HttpGet] [Route("_getClaimsOfRole")] [ResponseType(typeof(List))] public async Task GetClaimsOfRoleAsync(FhtUserRoleType role) { List claims =await _roleClaimQuery.GetClaimsOfRoleAsync(role); return Ok(claims); } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.ManageUser)] [ResponseType(typeof(List))] [Route("_getRoleTypes")] public IHttpActionResult GetRoleTypes(List excludeRoleTypes = null) { IOrderedEnumerable> fhtUserRoleTypes = EnumHelpers.Enum2Dictionary().OrderBy(m => m.Key); if (excludeRoleTypes != null && excludeRoleTypes.Any()) { fhtUserRoleTypes = (IOrderedEnumerable>)fhtUserRoleTypes.Where(rt => excludeRoleTypes.Contains(rt.Key)); } var userRoleTypes = new List(); fhtUserRoleTypes.Where(r => (r.Key != FhtUserRoleType.Administrator && r.Key != FhtUserRoleType.AgentAdmin && r.Key != FhtUserRoleType.AgentCustomerServiceStaff)).ToList().ForEach(et => { userRoleTypes.Add(new SelectListItemViewModel { Value = (int)et.Key, Text = et.Key.GetEnumDescription() }); }); return Ok(userRoleTypes); } [HttpGet] [ResponseType(typeof(UserViewModel))] [Route("myProfile")] public async Task MyProfileAsync() { int authenticatedUserId = User.Identity.GetUserId(); UserViewModel me = await _userQuery.GetAsync(authenticatedUserId); return Ok(me); } [ClaimsAuthorize(FhtUserClaimType.ManageUser)] [HttpGet] [Route("{userId:int:min(1)}")] [ResponseType(typeof(UpdateUserInputModel))] public IHttpActionResult Get(int userId) { UpdateUserInputModel model = _accountService.GetUserForUpdate(userId); return Ok(model); } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.ManageUser)] [ResponseType(typeof(QueryResult))] [Route("_search")] public async Task SearchUsersAsync(FhtUserRoleType? role = null, string q = "", int currPage = 1, int limit = 20) { int offset = (currPage < 2 ? 0 : currPage - 1) * limit; QueryResult res = await _userQuery.SearchUsersAsync(role, q, offset, limit); return Ok(res); } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.ManageChannelUser)] [ResponseType(typeof(QueryResult))] [Route("_searchChannelUser")] public async Task SearchChannelUsersAsync(FhtUserRoleType? role = null, int? parentId = null, string q = "", int currPage = 1, int limit = 20) { int authenticatedUserId = User.Identity.GetUserId(); int offset = (currPage < 2 ? 0 : currPage - 1) * limit; QueryResult res = await _userQuery.SearchChannelUsersAsync(authenticatedUserId, role, parentId, q, offset, limit); return Ok(res); } [ClaimsAuthorize(FhtUserClaimType.ViewChannelUser)] [HttpGet] [ResponseType(typeof(QueryResult))] [Route("_subordinateChannelUsers")] public async Task SubordinateChannelUsersAsync(int? parentId = null, string q = "", int currPage = 1, int limit = 20) { int authenticatedUserId = User.Identity.GetUserId(); int offset = (currPage < 2 ? 0 : currPage - 1) * limit; int pId = parentId ?? authenticatedUserId; QueryResult res = await _userQuery.SearchSubordinateChannelUsersAsync(pId, q, offset, limit); return Ok(res); } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.ManageChannelUser)] [ResponseType(typeof(List))] [Route("_getChannelUsers")] public async Task GetChannelUsersAsync(int? parentId = null) { int authenticatedUserId = User.Identity.GetUserId(); List channelManagers = (await _userQuery.GetAllChannelUsersAsync(authenticatedUserId)).Where(u => u.Role != FhtUserRoleType.ChannelManager).ToList(); List res = ChannelUserHelper.ChannelUser(channelManagers, User.UserRole(), parentId); return Ok(res); } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.ViewChannelUser, FhtUserClaimType.CreateChannelUser, FhtUserClaimType.ManageChannelUser)] [ResponseType(typeof(List))] [Route("_getUsersOfRole")] public async Task GetUsersOfRole(FhtUserRoleType role) { int authenticatedUserId = User.Identity.GetUserId(); var res = new List(); if (role == FhtUserRoleType.ChannelSupervisor)//渠道总监无上级 { return Ok(res); } if (role == FhtUserRoleType.RegionalChannelMananger) { role = FhtUserRoleType.ChannelSupervisor; } else if (role == FhtUserRoleType.ChannelManager) { role = FhtUserRoleType.RegionalChannelMananger; } List users = await _userQuery.GetUsersOfRoleAsync(role); res = users.Select(r => new SelectListItemViewModel { Value = r.UserId, Text = r.DescriptiveName }).ToList(); return Json(res); } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.ManageChannelUser)] [ResponseType(typeof(AllotChannelUserInputModel))] [Route("_getAllotChannelUser")] public async Task GetAllotChannelUserAsync(int userId) { UserViewModel user = await _userQuery.GetAsync(userId); if (user != null && (user.Role == FhtUserRoleType.ChannelManager || user.Role == FhtUserRoleType.RegionalChannelMananger) && user.ParentId.HasValue) { throw new Exception("请勿非法操作!"); } UserViewModel currentParent = await _userQuery.GetAsync(user.ParentId.Value); var allotChannelUserInputModel = new AllotChannelUserInputModel { UserId = user.UserId, DescriptiveName = user.DescriptiveName, UserName = user.UserName, Mobile = user.Mobile, CurrentParent = currentParent != null ? currentParent.DescriptiveName : "", Role = user.Role }; return Ok(allotChannelUserInputModel); } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.ManageChannelUser)] [ResponseType(typeof(List))] [Route("_getParentUsers")] public async Task GetParentUsersAsync(int userId) { UserViewModel user = await _userQuery.GetAsync(userId); var res = new List(); if (user.ParentId.HasValue) { UserViewModel currentParent = await _userQuery.GetAsync(user.ParentId.Value); List parentUsers = await _userQuery.GetUsersOfRoleAsync(currentParent.Role); res = parentUsers.Where(u => u.UserId != currentParent.UserId).ToList(); } return Ok(res); } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.ManageChannelUser)] [ResponseType(typeof(List))] [Route("_getAllCustomerServices")] public async Task GetAllCustomerServicesAsync() { List customerServiceStaffs = await _userQuery.GetUsersOfRoleAsync(FhtUserRoleType.CustomerServiceStaff); List res = customerServiceStaffs.Select(c => new SelectListItemViewModel { Value = c.UserId, Text = c.DescriptiveName }).ToList(); return Ok(res); } #endregion #region 修改 [ClaimsAuthorize(FhtUserClaimType.RoleClaimManage)] [HttpPost] [ValidateModel] [Route("_addClaimToRole")] [ResponseType(typeof(void))] public IHttpActionResult AddClaimToRole(AddOrRemoveClaimInputModel model) { _roleClaimService.AddClaimToRole(model); return Ok(); } [HttpPost] [ClaimsAuthorize(FhtUserClaimType.RoleClaimManage)] [ValidateModel] [Route("_removeClaimFromRole")] [ResponseType(typeof(void))] public IHttpActionResult RemoveClaimFromRole(AddOrRemoveClaimInputModel model) { _roleClaimService.RemoveClaimFromRole(model); return Ok(); } [ClaimsAuthorize(FhtUserClaimType.ManageUser)] [HttpPost] [ValidateModel] [Route("_create", Name = nameof(CreateAccountAsync))] [ResponseType(typeof(CreatedViewModel))] public async Task CreateAccountAsync(CreateAccountInputModel model) { int userId = User.Identity.GetUserId(); if (model.Role == FhtUserRoleType.Administrator) { throw new ArgumentException("不可创建超级管理员!"); } int result = await _accountService.CreateAsync(userId, model); string url = Url.Link(nameof(CreateAccountAsync), new { result }); return Created(url, new CreatedViewModel { Id = result, Location = url }); } [HttpPost] [ValidateModel] [ClaimsAuthorize(FhtUserClaimType.ManageUser)] [Route("_update")] [ResponseType(typeof(void))] public async Task UpdateUserAsync(UpdateUserInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); await _accountService.UpdateAsync(authenticatedUserId, model); return Ok(); } [ClaimsAuthorize(FhtUserClaimType.CreateChannelUser)] [HttpPost] [ValidateModel] [ResponseType(typeof(CreatedViewModel))] [Route("_createChannelUser")] public async Task CreateChannelUserAsync(CreateAccountInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); int result = await _accountService.CreateChannelAsync(authenticatedUserId, model); string url = Url.Link(nameof(CreateAccountAsync), new { result }); return Created(url, new CreatedViewModel { Id = result, Location = url }); } [ClaimsAuthorize(FhtUserClaimType.ManageChannelUser)] [HttpPost] [ValidateModel] [ResponseType(typeof(void))] [Route("_updateChannelUser")] public async Task UpdateChannelUserAsync(UpdateUserInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); await _accountService.UpdateChannelUserAsync(authenticatedUserId, model); return Ok(); } [ClaimsAuthorize(FhtUserClaimType.ManageChannelUser)] [HttpPost] [ValidateModel] [ResponseType(typeof(void))] [Route("_allotChannelUser")] public IHttpActionResult AllotChannelUser(AllotChannelUserInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); _accountService.AllotChannelUser(authenticatedUserId, model); return Ok(); } [HttpPost] [ClaimsAuthorize(FhtUserClaimType.ManageChannelUser)] [ResponseType(typeof(void))] [Route("_setCustomerService")] public IHttpActionResult SetCustomerService(int userId, int customerServiceId) { _accountService.SetCustomerService(userId, customerServiceId); return Ok(); } #endregion #region autocomplete [HttpGet] [Route("_autocompleteschanneluser")] [ResponseType(typeof(List))] public async Task SearchChannelUsersAsync(string q = "", int? id = null) { int authenticatedUserId = User.Identity.GetUserId(); var users = new List(); if (id.HasValue) { UserViewModel currUser = await _userQuery.GetAsync(id.Value); users.Add(new SelectListItemViewModel { Text = currUser.DescriptiveName, Value = currUser.UserId }); } else { UserViewModel currUser = await _userQuery.GetAsync(authenticatedUserId); List allChannelUsers = await _userQuery.GetCustomerServiceChannelUsersAsync(authenticatedUserId); allChannelUsers = allChannelUsers.Where(u => u.DescriptiveName.Contains(q)).Select(u => u).ToList(); if (User.UserRole() == FhtUserRoleType.CustomerServiceStaff) { users = ChannelUserHelper.GetCustomerServiceChannelUsers(allChannelUsers, id); } else { users = ChannelUserHelper.GetChannelUser(allChannelUsers, currUser.Role, id); } } return Ok(users); } #endregion } }