public override void OnActionExecuting(HttpActionContext actionContext) { try { var userManager = Startup.UserManagerFactory(); var tokenObj = new ParsedTokenHelper().GetParsedToken(actionContext.Request.Properties); var userId = tokenObj.UserId; if (Roles.Any(role => userManager.IsInRole(userId, role.ToString()))) { return; } actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized); } catch (TokenExpiredException) { actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, ResponseBase.TokenExpired()); } catch (BankClientException ex) { actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, ResponseBase.Unsuccessful(ex)); } catch (Exception ex) { actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, ResponseBase.Unsuccessful(ex)); } }
public IHttpActionResult GetByCustomerId(int? page = null) { try { var tokenObj = new ParsedTokenHelper().GetParsedToken(Request.Properties); const int pageSize = 10; var pageNumber = page ?? 1; var result = _customerCreditService.GetAllByUser(tokenObj.UserId, pageNumber, pageSize); return Ok(result); } catch (BankClientException ex) { return BadRequest(ex.Message); } catch (Exception ex) { return InternalServerError(ex); } }
public IHttpActionResult ChangeEmail(ChangeEmailBindingModel model) { try { if (!ModelState.IsValid) { return BadRequest(ModelState); } var tokenObj = new ParsedTokenHelper().GetParsedToken(Request.Properties); var user = UserManager.FindById(tokenObj.UserId); if (user != null && user.Email == model.NewEmail) { return BadRequest("Current email is equal to entered."); } if (UserManager.FindByEmail(model.NewEmail) != null) { return BadRequest("User with specified email is already registered."); } var baseUrl = String.Format("{0}://{1}", Request.RequestUri.Scheme, Request.RequestUri.Authority); _iAuthenticationService.ChangeEmail(tokenObj.UserId, model.NewEmail, baseUrl); return Ok(); } catch (BankClientException ex) { return BadRequest(ex.Message); } catch (Exception ex) { return InternalServerError(ex); } }
public async Task<IHttpActionResult> ChangePassword(ChangePasswordBindingModel model) { try { if (!ModelState.IsValid) { return BadRequest(ModelState); } var tokenObj = new ParsedTokenHelper().GetParsedToken(Request.Properties); IdentityResult result = await UserManager.ChangePasswordAsync(tokenObj.UserId, model.OldPassword, model.NewPassword); IHttpActionResult errorResult = GetErrorResult(result); if (errorResult != null) { return errorResult; } return Ok(); } catch (BankClientException ex) { return BadRequest(ex.Message); } catch (Exception ex) { return InternalServerError(ex); } }
public IHttpActionResult GetRole() { try { var tokenObj = new ParsedTokenHelper().GetParsedToken(Request.Properties); var role = UserManager.GetRoles(tokenObj.UserId).FirstOrDefault(); return Ok(new GetRoleResponse() { Role = role }); } catch (BankClientException ex) { return BadRequest(ex.Message); } catch (Exception ex) { return InternalServerError(ex); } }
public override void OnActionExecuting(HttpActionContext actionContext) { try { var requestScope = actionContext.Request.GetDependencyScope(); var authenticationService = requestScope.GetService(typeof(IAuthenticationService)) as IAuthenticationService; if (authenticationService == null) { throw BankClientException.ThrowAutofacError("AuthenticationService is null"); } var token = actionContext.Request.Headers.First(p => p.Key.ToLower() == "token").Value.First(); var parsedToken = authenticationService.CheckToken(token); actionContext.Request.Properties.Add("tokenObj", parsedToken); if (Roles != null) { var userManager = Startup.UserManagerFactory(); var tokenObj = new ParsedTokenHelper().GetParsedToken(actionContext.Request.Properties); var userId = tokenObj.UserId; if (Roles.Any(role => userManager.IsInRole(userId, role.ToString()))) { return; } actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized); } } catch (TokenExpiredException) { // var logService = actionContext.Request.GetDependencyScope().GetService(typeof(ILogService)) as ILogService; // if (logService == null) // { // throw BankClientException.ThrowAutofacError("LogService is null"); // } // logService.Log("Token expired", "CheckToken", LogType.Warning); actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, ResponseBase.TokenExpired()); } catch (BankClientException ex) { // var logService = actionContext.Request.GetDependencyScope().GetService(typeof(ILogService)) as ILogService; // if (logService == null) // { // throw BankClientException.ThrowAutofacError("LogService is null"); // } // logService.Log(ex.ToString(), "CheckToken", LogType.Error); actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, ResponseBase.Unsuccessful(ex)); } catch (Exception ex) { // var logService = actionContext.Request.GetDependencyScope().GetService(typeof(ILogService)) as ILogService; // if (logService == null) // { // throw BankClientException.ThrowAutofacError("LogService is null"); // } // logService.Log(ex.ToString(), "CheckToken", LogType.Error); actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, ResponseBase.Unsuccessful(ex)); } }
public IHttpActionResult GetConfirmed(int? page = null) { try { var chiefRole = _roleManager.Roles.FirstOrDefault(r => r.Name == AppRoles.CreditDepartmentChief.ToString()); var tokenObj = new ParsedTokenHelper().GetParsedToken(Request.Properties); const int pageSize = 10; var pageNumber = page ?? 1; var сonfirmedCreditRequests = _iCreditRequestService.GetConfirmed(tokenObj.UserId, chiefRole, pageNumber, pageSize); return Ok(new GetConfirmedCreditResponse() { CreditRequests = Mapper.Map<CustomPagedList<ShortCreditRequest>>(сonfirmedCreditRequests) }); } catch (BankClientException ex) { return BadRequest(ex.Message); } catch (Exception ex) { return InternalServerError(ex); } }
public IHttpActionResult SetStatus(SetStatusRequest request) { try { var tokenObj = new ParsedTokenHelper().GetParsedToken(Request.Properties); _iCreditRequestService.SetStatus(tokenObj.UserId, request.CreditRequestId, request.CreditRequestStatusInfo, request.Message); return Ok(); } catch (BankClientException ex) { return BadRequest(ex.Message); } catch (Exception ex) { return InternalServerError(ex); } }
public IHttpActionResult GetUnconfirmedByChief(int? page = null) { try { var tokenObj = new ParsedTokenHelper().GetParsedToken(Request.Properties); var roleName = _userManager.GetRoles(tokenObj.UserId).FirstOrDefault(); var role = _roleManager.FindByName(roleName); const int pageSize = 10; var pageNumber = page ?? 1; var unconfirmedCreditRequests = _iCreditRequestService.GetUnconfirmedByChief(role, pageNumber, pageSize); return Ok(new GetUnconfirmedCreditResponse() { CreditRequests = Mapper.Map<CustomPagedList<ShortCreditRequest>>(unconfirmedCreditRequests) }); } catch (BankClientException ex) { return BadRequest(ex.Message); } catch (Exception ex) { return InternalServerError(ex); } }
public IHttpActionResult GetConfirmedByChief(int? page = null) { try { var tokenObj = new ParsedTokenHelper().GetParsedToken(Request.Properties); const int pageSize = 10; var pageNumber = page ?? 1; var сonfirmedCreditRequests = _iCreditRequestService.GetConfirmedByChief(tokenObj.UserId, pageNumber, pageSize); return Ok(new GetConfirmedCreditResponse() { CreditRequests = Mapper.Map<CustomPagedList<ShortCreditRequest>>(сonfirmedCreditRequests) }); } catch (BankClientException ex) { return BadRequest(ex.Message); } catch (Exception ex) { return InternalServerError(ex); } }