using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Web.Mvc; 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.Filters; using FHT.Utilities; using Microsoft.AspNet.Identity; using WebGrease.Css.Extensions; using System.Net; using FHT.Agency.Web.Extensions; using FHT.Agency.Web.Utility; using FHT.Agency.UI.InputModels.Search; using FHT.MvcCommon; namespace FHT.Agency.Web.Controllers { public class AuthorizationController : Controller { private readonly IRoleClaimService _roleClaimService; private readonly IRoleClaimQuery _roleClaimQuery; private readonly IAccountService _accountService; private readonly IUserQuery _userQuery; public AuthorizationController(IRoleClaimService roleClaimService, IRoleClaimQuery roleClaimQuery, IAccountService accountService, IUserQuery userQuery) { _roleClaimService = roleClaimService; _roleClaimQuery = roleClaimQuery; _accountService = accountService; _userQuery = userQuery; } [HttpGet] public ActionResult Index() { return View(); } [ClaimsAuthorize(FhtUserClaimType.RoleClaimManage)] [HttpGet] public async Task Roles() { List roleClaims =await _roleClaimQuery.GetAllRoleClaimsAsync(); return View(roleClaims); } [ClaimsAuthorize(FhtUserClaimType.RoleClaimManage)] [HttpGet] public async Task Role(FhtUserRoleType role) { ViewBag.ExistingClaims = await _roleClaimQuery.GetClaimsOfRoleAsync(role); return View(new AddOrRemoveClaimInputModel { Role = role }); } [ClaimsAuthorize(FhtUserClaimType.RoleClaimManage)] [HttpPost] [ValidateAntiForgeryToken] public ActionResult AddClaimToRole(AddOrRemoveClaimInputModel model) { if (model == null || !ModelState.IsValid) { return View("Role"); } _roleClaimService.AddClaimToRole(model); return RedirectToAction(nameof(Role), new { role = model.Role }); } [HttpPost] [ClaimsAuthorize(FhtUserClaimType.RoleClaimManage)] [ValidateAntiForgeryToken] public ActionResult RemoveClaimFromRole(AddOrRemoveClaimInputModel model) { if (model == null || !ModelState.IsValid) { return View("Role"); } _roleClaimService.RemoveClaimFromRole(model); return RedirectToAction(nameof(Role), new { role = model.Role }); } [ClaimsAuthorize(FhtUserClaimType.ManageUser)] [HttpGet] public ActionResult CreateUser() { IOrderedEnumerable> fhtUserRoleTypes = EnumHelpers.Enum2Dictionary().OrderBy(m => m.Key); var userRoleTypes = new List(); fhtUserRoleTypes.Where(r => (r.Key != FhtUserRoleType.Administrator && r.Key != FhtUserRoleType.AgentAdmin && r.Key != FhtUserRoleType.AgentCustomerServiceStaff)).ForEach(et => { userRoleTypes.Add(new SelectListItem { Value = ((int)et.Key).ToString(), Text = et.Key.GetEnumDescription(), }); }); ViewBag.UserRoleTypes = userRoleTypes; return View(new CreateAccountInputModel()); } [ClaimsAuthorize(FhtUserClaimType.ManageUser)] [HttpPost] [ValidateAntiForgeryToken] public async Task CreateUser(CreateAccountInputModel model) { int userId = User.Identity.GetUserId(); if (!ModelState.IsValid) { return View(); } if (model.Role == FhtUserRoleType.Administrator) { throw new ArgumentException("不可创建超级管理员!"); } int result = await _accountService.CreateAsync(userId, model); if (result > 0) { return RedirectToAction(nameof(Users), "Authorization"); } return View(model); } [ClaimsAuthorize(FhtUserClaimType.ManageUser)] [HttpGet] public ActionResult UpdateUser(int userId) { UpdateUserInputModel model = _accountService.GetUserForUpdate(userId); return View(model); } [HttpPost] [ValidateAntiForgeryToken] [ClaimsAuthorize(FhtUserClaimType.ManageUser)] public async Task UpdateUser(UpdateUserInputModel model) { if (!ModelState.IsValid) { return View(); } int authenticatedUserId = User.Identity.GetUserId(); await _accountService.UpdateAsync(authenticatedUserId, model); return RedirectToAction(nameof(Users), "Authorization"); } [ClaimsAuthorize(FhtUserClaimType.ManageUser)] [HttpGet] public async Task Users(SearchUserInputModel searchUserInputModel) { ViewBag.Users =await _userQuery.SearchUsersAsync(searchUserInputModel.Role, searchUserInputModel.UserName, searchUserInputModel.Offset(), searchUserInputModel.Limit); return View(searchUserInputModel); } [ClaimsAuthorize(FhtUserClaimType.ManageChannelUser)] [HttpGet] public async Task< ActionResult> ChannelUsers(SearchChannelUserInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); List channelManagers =(await _userQuery.GetAllChannelUsersAsync(authenticatedUserId)).Where(u => u.Role != FhtUserRoleType.ChannelManager).ToList(); ViewBag.ChannelManagers = ChannelUserHelper.ChannelUser(channelManagers, User.UserRole(), model.ParentId); ViewBag.Users =await _userQuery.SearchChannelUsersAsync(authenticatedUserId, model.Role, model.ParentId, model.RealName, model.Offset(), model.Limit); return View(model); } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.ManageChannelUser)] public ActionResult CreateChannelUser() { var model = new CreateAccountInputModel(); IOrderedEnumerable> fhtUserRoleTypes = EnumHelpers.Enum2Dictionary().OrderBy(m => m.Key); var roles = new List(); fhtUserRoleTypes.Where(r => r.Key == FhtUserRoleType.ChannelManager || r.Key == FhtUserRoleType.ChannelSupervisor || r.Key == FhtUserRoleType.RegionalChannelMananger).ForEach(et => { roles.Add(new SelectListItem { Value = ((int)et.Key).ToString(), Text = et.Key.GetEnumDescription() }); }); ViewBag.Roles = roles; return View(model); } [HttpPost] [ClaimsAuthorize(FhtUserClaimType.ManageChannelUser)] [ValidateAntiForgeryToken] public async Task CreateChannelUser(CreateAccountInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); if (model != null && ModelState.IsValid) { int result = await _accountService.CreateChannelAsync(authenticatedUserId, model); if (result > 0) { return RedirectToAction(nameof(ChannelUsers), "Authorization"); } } return View(model); } [ClaimsAuthorize(FhtUserClaimType.CreateChannelUser)] [HttpGet] public ActionResult UpdateChannelUser(int userId) { UpdateUserInputModel model = _accountService.GetUserForUpdate(userId); ViewBag.Roles = GetUserRoles(model.Role); return View(model); } [ClaimsAuthorize(FhtUserClaimType.ManageChannelUser)] [HttpPost] [ValidateAntiForgeryToken] public async Task UpdateChannelUser(UpdateUserInputModel model) { try { if (!ModelState.IsValid) { return View(); } int authenticatedUserId = User.Identity.GetUserId(); await _accountService.UpdateChannelUserAsync(authenticatedUserId, model); return RedirectToAction(nameof(ChannelUsers), "Authorization"); } #pragma warning disable FHTRoslynChecker_ExceptionCheck // Do not catch Exceptions which are not defined by FHT catch (InvalidOperationException ex) #pragma warning restore FHTRoslynChecker_ExceptionCheck // Do not catch Exceptions which are not defined by FHT { ModelState.AddModelError("", ex.Message); ViewBag.Roles = GetUserRoles(model.Role); return View(model); } } private List GetUserRoles(FhtUserRoleType currentRole) { var roles = new List(); IOrderedEnumerable> fhtUserRoleTypes = EnumHelpers.Enum2Dictionary().OrderBy(m => m.Key); fhtUserRoleTypes.Where(r => r.Key == FhtUserRoleType.ChannelManager || r.Key == FhtUserRoleType.ChannelSupervisor || r.Key == FhtUserRoleType.RegionalChannelMananger).ForEach(et => { roles.Add(new SelectListItem { Value = ((int)et.Key).ToString(), Text = et.Key.GetEnumDescription(), Selected = et.Key == currentRole }); }); return roles; } [ClaimsAuthorize(FhtUserClaimType.ViewChannelUser)] [HttpGet] public async Task SubordinateChannelUsers(SearchChannelUserInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); int parentId = model.ParentId.HasValue ? model.ParentId.Value : authenticatedUserId; ViewBag.Users =await _userQuery.SearchSubordinateChannelUsersAsync(parentId, model.RealName, model.Offset(), model.Limit); return View(model); } [HttpGet] [IsAjaxJsonResult] [ClaimsAuthorize(FhtUserClaimType.ViewChannelUser, FhtUserClaimType.CreateChannelUser, FhtUserClaimType.ManageChannelUser)] public async Task GetUsersOfRole(FhtUserRoleType role) { int authenticatedUserId = User.Identity.GetUserId(); if (role == FhtUserRoleType.ChannelSupervisor)//渠道总监无上级 { return Json(new List(), JsonRequestBehavior.AllowGet); } if (role == FhtUserRoleType.RegionalChannelMananger) { role = FhtUserRoleType.ChannelSupervisor; } else if (role == FhtUserRoleType.ChannelManager) { role = FhtUserRoleType.RegionalChannelMananger; } List users =await _userQuery.GetUsersOfRoleAsync(role); IEnumerable channelUsers = users.Select(r => new SelectListItemViewModel { Value = r.UserId, Text = r.DescriptiveName }); return Json(channelUsers, JsonRequestBehavior.AllowGet); } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.ManageChannelUser)] public async Task AllotChannelUser(int userId) { UserViewModel user =await _userQuery.GetAsync(userId); if (user != null && (user.Role == FhtUserRoleType.ChannelManager || user.Role == FhtUserRoleType.RegionalChannelMananger) && user.ParentId.HasValue) { 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 }; List parentUsers =await _userQuery.GetUsersOfRoleAsync(currentParent.Role); ViewBag.ChannelManagers = parentUsers.Where(u => u.UserId != currentParent.UserId); return View(allotChannelUserInputModel); } else { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } } [ClaimsAuthorize(FhtUserClaimType.ManageChannelUser)] [HttpPost] [ValidateAntiForgeryToken] public ActionResult AllotChannelUser(AllotChannelUserInputModel model) { if (!ModelState.IsValid) { return View(model); } int authenticatedUserId = User.Identity.GetUserId(); _accountService.AllotChannelUser(authenticatedUserId, model); return RedirectToAction(nameof(ChannelUsers)); } [HttpGet] [ClaimsAuthorize(FhtUserClaimType.ManageChannelUser)] public async Task SetCustomerService(int userId) { if (User.UserRole() != FhtUserRoleType.CustomerServiceManager) { return new HttpStatusCodeResult(HttpStatusCode.Unauthorized); } UserViewModel currentUser =await _userQuery.GetAsync(userId); List customerServiceStaffs =await _userQuery.GetUsersOfRoleAsync(FhtUserRoleType.CustomerServiceStaff); var customerServices = new List(); customerServiceStaffs.ForEach(css => { customerServices.Add(new SelectListItem { Value = css.UserId.ToString(), Text = css.DescriptiveName, Selected = css.UserId == currentUser.CustomerServiceId }); }); ViewBag.CustomerServices = customerServices; return View(currentUser); } [HttpPost] [ClaimsAuthorize(FhtUserClaimType.ManageChannelUser)] public ActionResult SetCustomerService(int userId, int customerServiceId) { _accountService.SetCustomerService(userId, customerServiceId); return RedirectToAction(nameof(ChannelUsers)); } } }