using System.Web.Http; using System.Web.Http.Description; using FHT.Agency.Services.ImplementedInterfaces; using FHT.Agency.Queries.ImplementedInterfaces; using FHT.Agency.UI.InputModels.Search; using FHT.Agency.UI.ViewModels; using System.Collections.Generic; using FHT.Agency.Web.Extensions; using FHT.Agency.Web.Filters; using FHT.Agency.CommonTypes; using FHT.ApiCommon; using FHT.Agency.UI.InputModels; using System.Threading.Tasks; using Microsoft.AspNet.Identity; using System; using FHT.Core.UI.ViewModels; using FHT.DDD; using System.Web.Http.Cors; using FHT.Agency.Services.ImplementedInterfaces.Exceptions; namespace FHT.Agency.Web.ApiControllers { [Authorize] [RoutePrefix("api/authorizations")] [EnableCors(origins: "*", headers: "*", methods: "*")] public class AuthorizationApiController : ApiController { private readonly IRoleClaimService _roleClaimService; private readonly IRoleClaimQuery _roleClaimQuery; private readonly IAccountService _accountService; private readonly IUserQuery _userQuery; public AuthorizationApiController(IRoleClaimService roleClaimService, IRoleClaimQuery roleClaimQuery, IAccountService accountService, IUserQuery userQuery) { _roleClaimService = roleClaimService; _roleClaimQuery = roleClaimQuery; _accountService = accountService; _userQuery = userQuery; } [HttpGet] [Route("myClaims")] [ResponseType(typeof(List))] public async Task< IHttpActionResult> MyClaimsAsync() { FhtUserRoleType role = User.UserRole(); List claims =await _roleClaimQuery.GetClaimsOfRoleAsync(role); return Ok(claims); } [HttpGet] [ResponseType(typeof(QueryResult))] [Route("_search")] [ApiClaimsAuthorize(FhtUserClaimType.ManageUser)] [ValidateModel] public async Task SearchUsersAsync([FromUri]SearchUserInputModel searchUserInputModel) { QueryResult users = await _userQuery.SearchUsersAsync(searchUserInputModel.Role, searchUserInputModel.UserName, searchUserInputModel.Offset(), searchUserInputModel.Limit); return Ok(users); } [HttpGet] [ResponseType(typeof(List))] [Route("roleClaims")] [ApiClaimsAuthorize(FhtUserClaimType.RoleClaimManage)] public async Task RoleClaimsAsync() { List roleClaims =await _roleClaimQuery.GetAllRoleClaimsAsync(); return Ok(roleClaims); } [HttpPost] [ResponseType(typeof(void))] [Route("_addClaim")] [ValidateModel] [ApiClaimsAuthorize(FhtUserClaimType.RoleClaimManage)] public IHttpActionResult AddClaims(AddOrRemoveClaimInputModel model) { _roleClaimService.AddClaimToRole(model); return Ok(); } [HttpPost] [ResponseType(typeof(void))] [Route("_removeClaim")] [ValidateModel] [ApiClaimsAuthorize(FhtUserClaimType.RoleClaimManage)] public IHttpActionResult RemoveClaim(AddOrRemoveClaimInputModel model) { _roleClaimService.RemoveClaimFromRole(model); return Ok(); } [HttpGet] [ResponseType(typeof(UpdateUserInputModel))] [Route("getUserForUpdate")] [ApiClaimsAuthorize(FhtUserClaimType.RoleClaimManage)] public IHttpActionResult GetUserForUpdate(int userId) { UpdateUserInputModel model = _accountService.GetUserForUpdate(userId); return Ok(model); } [HttpPost] [ResponseType(typeof(CreatedViewModel))] [Route("_createUser", Name = nameof(CreateUserAsync))] [ValidateModel] [ApiClaimsAuthorize(FhtUserClaimType.ManageUser)] public async Task CreateUserAsync(CreateAccountInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); int createdUserId = await _accountService.CreateAsync(authenticatedUserId, model); string url = Url.Link(nameof(CreateUserAsync), new { }); return Created(url, new CreatedViewModel { Id = createdUserId, Location = url }); } [HttpPost] [ResponseType(typeof(void))] [Route("_updateUser")] [ValidateModel] [ApiClaimsAuthorize(FhtUserClaimType.ManageUser)] public async Task UpdateUserAsync(UpdateUserInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); await _accountService.UpdateAsync(authenticatedUserId, model); return Ok(); } [HttpPost] [ResponseType(typeof(StringViewModel))] [Route("_resetPassword")] [ApiClaimsAuthorize(FhtUserClaimType.SuperResetPassword)] public async Task ResetPassWordAsync(int userId) { string password = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 6); await _accountService.ResetPassWordAsync(userId, password); return Ok(new StringViewModel { Str = password }); } [HttpPost] [ResponseType(typeof(void))] [Route("_changePassword")] [ValidateModel] public async Task ChangePasswordAsync(ChangePasswordInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); await _accountService.ChangePasswordAsync(authenticatedUserId, model); return Ok(); } [HttpGet] [ResponseType(typeof(UpdateUserInputModel))] [Route("_getMyProfile")] public IHttpActionResult GetMyProfile() { int authenticatedUserId = User.Identity.GetUserId(); UpdateUserInputModel model = _accountService.GetUserForUpdate(authenticatedUserId); return Ok(model); } /// /// 修改个人资料 /// /// /// [HttpPost] [ResponseType(typeof(void))] [Route("_changeUserProfile")] [ValidateModel] public IHttpActionResult ChangeUserProfile(UpdateUserInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); _accountService.ChangeUserProfile(model); return Ok(); } [HttpPost] [AllowAnonymous] [ValidateModel] [ResponseType(typeof(void))] [Route("_login")] public async Task LoginAsync(LoginInputModel model) { try { await _accountService.LoginAsync(model); return Ok(); } catch (WrongUserNameOrPasswordException) { throw new Exception("用户名或者密码错误!"); } catch (UserAccountRejectedException) { throw new Exception("用户未通过审核!"); } catch (AgentAccountRejectedException) { throw new Exception("代理商未通过审核!"); } catch (AgentContractExpiredException) { throw new Exception("代理商合同已到期!"); } } [HttpPost] [ResponseType(typeof(void))] [Route("_logOff")] public IHttpActionResult LogOff() { int authenticatedUserId = User.Identity.GetUserId(); _accountService.LogOff(authenticatedUserId); return Ok(); } } }