public async Task <UserEnvelope> Handle(Query request, CancellationToken cancellationToken) { var currentUsername = _currentUserAccessor.GetCurrentUsername(); var person = await _context.Persons .AsNoTracking() .FirstOrDefaultAsync(x => x.Username == request.Username, cancellationToken); if (person == null) { throw new RestException(HttpStatusCode.NotFound, new { User = Constants.NOT_FOUND }); } if (_currentUserAccessor.GetCurrentUserType().Equals(UserConstants.User) && !currentUsername.Equals(person.Username) ) { throw new RestException(HttpStatusCode.Unauthorized, new { User = Constants.UNAUTHERIZE }); } var user = _mapper.Map <Person, User>(person); user.Type = UserConstants.GetUserTypeString(person.UserType); if (!user.Username.Equals(currentUsername)) { return(new UserEnvelope(user)); } // To mark the profile UI is current user user.IsCurrentUser = true; return(new UserEnvelope(user)); }
public async Task <UserEnvelope> Handle(Command request, CancellationToken cancellationToken) { if (_currentUserAccessor.GetCurrentUserType().Equals(UserConstants.User)) { throw new RestException(HttpStatusCode.Unauthorized, new { Username = Constants.UNAUTHERIZE }); } if (await _context.Persons.Where(x => x.Username == request.User.Username).AsNoTracking().AnyAsync(cancellationToken)) { throw new RestException(HttpStatusCode.BadRequest, new { Username = Constants.IN_USE }); } var person = new Person { Username = request.User.Username, ProfileUrl = request.User.ProfileUrl, Phone = request.User.Phone, UserType = request.User.UserType, Password = _passwordHasher.Hash(request.User.Password), CreatedDate = DateTime.Now }; await _context.Persons.AddAsync(person, cancellationToken); await _context.SaveChangesAsync(cancellationToken); var auditId = _currentUserAccessor.GetAuditId(); var user = _mapper.Map <Person, User>(person); user.Type = UserConstants.GetUserTypeString(person.UserType); user.Token = _jwtTokenGenerator.CreateToken(user.Username, user.Type, auditId.ToString()); return(new UserEnvelope(user)); }
public async Task <UserEnvelope> Handle(Command request, CancellationToken cancellationToken) { var person = await _context.Persons .Where(x => x.Username == request.User.Username) .AsNoTracking() .SingleOrDefaultAsync(cancellationToken); if (person == null) { throw new RestException(HttpStatusCode.Unauthorized, new { Error = "Invalid email / password." }); } if (!_passwordHasher.Verify(request.User.Password, person.Password)) { throw new RestException(HttpStatusCode.Unauthorized, new { Error = "Invalid email / password." }); } var user = _mapper.Map <Person, User>(person); user.Type = UserConstants.GetUserTypeString(person.UserType); user.IsCurrentUser = true; /* * Trace user login */ var userAgent = _currentUserAccessor.GetUserAgent(); var description = $"LoggedIn using ip: {_currentUserAccessor.GetUserIp()}, " + $"UserAgent: {userAgent.OS} {userAgent.Name} {userAgent.Version}"; var audit = new Audit() { Descriptions = description, CreatedDate = DateTime.Now, PersonId = person.Id }; await _context.Audits.AddAsync(audit, cancellationToken); await _context.SaveChangesAsync(cancellationToken); user.Token = _jwtTokenGenerator.ValidTokenTime(ValidPeriodUser).CreateToken(person.Username, user.Type, audit.Id.ToString()); return(new UserEnvelope(user)); }