Ejemplo n.º 1
0
            public async Task <UserDto> Handle(Query request, CancellationToken cancellationToken)
            {
                IdentityUser identityUser = await _unitOfWork.IdentityUserRepo.FindFirstAsync(request.UserName, cancellationToken);

                if (identityUser == null)
                {
                    throw new CustomException(HttpStatusCode.BadRequest);
                }

                //If recently expired token is matching with token in request please return recently generated token
                //This will be the case when two concurrent http request comes from same client and one request updates refresh token
                else if (request.RefreshToken == identityUser.PreviousRefreshToken && identityUser.PreviousRefreshTokenExpiry > HelperFunc.GetCurrentDateTime())
                {
                    return(_mapperHelper.Map <IdentityUser, UserDto>(identityUser));
                }

                //Check if current refresh token is matching and valid
                else if (request.RefreshToken == identityUser.RefreshToken && identityUser.RefreshTokenExpiry > HelperFunc.GetCurrentDateTime())
                {
                    //If current valid token is expired, generate new token and return it.
                    //else return existing token that is still valid.
                    if (request.RefreshToken == identityUser.RefreshToken)
                    {
                        identityUser.PreviousRefreshToken       = identityUser.RefreshToken;
                        identityUser.PreviousRefreshTokenExpiry = HelperFunc.GetCurrentDateTime().AddMinutes(_PREVIOUS_REFRESH_TOKEN_EXPIRES_IN_SEC);

                        identityUser.RefreshToken       = _jwtGenerator.CreateRefreshToken();
                        identityUser.RefreshTokenExpiry = HelperFunc.GetCurrentDateTime().AddDays(_REFRESH_TOKEN_EXPIRS_IN_DAYS);
                        _unitOfWork.IdentityUserRepo.Update(identityUser);
                        await _unitOfWork.SaveAsync(cancellationToken);
                    }
                    return(_mapperHelper.Map <IdentityUser, UserDto>(identityUser));
                }
                throw new CustomException(HttpStatusCode.BadRequest);
            }
Ejemplo n.º 2
0
            public async Task <Guid> Handle(Command request, CancellationToken cancellationToken)
            {
                DataModel.Activity activity = _mapperHelper.Map <Command, DataModel.Activity>(request);
                //Generate new Id for new Entity
                activity.Id = Guid.NewGuid();
                _unitOfWork.ActivityRepo.Add(activity);

                UserActivity hostAttendee = new UserActivity
                {
                    Activity   = activity,
                    IsHost     = true,
                    DateJoined = HelperFunc.GetCurrentDateTime(),
                    AppUserId  = _userAccessor.GetCurrentUserId()
                };

                _unitOfWork.UserActivityRepo.Add(hostAttendee);

                int insertCnt = await _unitOfWork.SaveAsync(cancellationToken);

                if (insertCnt > 0)
                {
                    return(activity.Id);
                }

                throw new Exception("Problem saving changes to database");
            }
Ejemplo n.º 3
0
        public CommentMapper()
        {
            Map <DataModel.Comment, CommentDto>()
            .ForMember(dest => dest.UserDisplayName, opt => opt.MapFrom(src => src.Author.DisplayName))
            .ForMember(dest => dest.UserImage, opt => opt.MapFrom <CommentPhotoUrlResolver>())
            .ForMember(dest => dest.UserId, opt => opt.MapFrom(src => src.Author.Id));

            Map <Create.Command, DataModel.Comment>(false)
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => Guid.NewGuid()))
            .ForMember(dest => dest.CreatedDate, opt => HelperFunc.GetCurrentDateTime());
        }
Ejemplo n.º 4
0
        public PhotoMapper()
        {
            Map <Photo.Add.Command, DataModel.Photo>()
            .ForMember(dest => dest.ContentType, opt => opt.MapFrom(src => src.File.ContentType))
            .ForMember(dest => dest.ActualFileName, opt => opt.MapFrom(src => src.File.FileName))
            .ForMember(dest => dest.Length, opt => opt.MapFrom(src => src.File.Length))
            .ForMember(dest => dest.UploadedDate, opt => opt.MapFrom(src => HelperFunc.GetCurrentDateTime()));

            Map <DataModel.Photo, PhotoDto>(false)
            .ForMember(dest => dest.Url, opt => opt.MapFrom <PhotoUrlResolver>());
        }
Ejemplo n.º 5
0
            public IdentityUser Resolve(Register.Command source, AppUser dest, IdentityUser destMember, ResolutionContext context)
            {
                string salt = _cryptoHelper.CreateBase64Salt();

                return(new IdentityUser
                {
                    UserName = source.UserName,
                    Salt = salt,
                    Passoword = _cryptoHelper.GenerateHash(source.Password, salt),
                    RefreshToken = _jwtGenerator.CreateRefreshToken(),
                    RefreshTokenExpiry = HelperFunc.GetCurrentDateTime().AddDays(30)
                });
            }
Ejemplo n.º 6
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                UserActivity hostAttendee = new UserActivity
                {
                    ActivityId = request.ActivityId,
                    DateJoined = HelperFunc.GetCurrentDateTime(),
                    AppUserId  = _userAccessor.GetCurrentUserId()
                };

                _unitOfWork.UserActivityRepo.Add(hostAttendee);

                int insertCnt = await _unitOfWork.SaveAsync(cancellationToken);

                if (insertCnt > 0)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes to database");
            }
Ejemplo n.º 7
0
            public async Task <UserDto> Handle(Command request, CancellationToken cancellationToken)
            {
                IdentityUser identityUser = await _unitOfWork.IdentityUserRepo.FindFirstAsync(request.UserName, cancellationToken);

                if (identityUser == null)
                {
                    throw new CustomException(HttpStatusCode.Unauthorized);
                }

                if (_cryptoHelper.GenerateHash(request.Password, identityUser.Salt) == identityUser.Passoword)
                {
                    identityUser.PreviousRefreshToken       = null;
                    identityUser.PreviousRefreshTokenExpiry = null;
                    identityUser.RefreshToken       = _jwtGenerator.CreateRefreshToken();
                    identityUser.RefreshTokenExpiry = HelperFunc.GetCurrentDateTime().AddDays(30);
                    _unitOfWork.IdentityUserRepo.Update(identityUser);

                    await _unitOfWork.SaveAsync(cancellationToken);

                    return(_mapperHelper.Map <IdentityUser, UserDto>(identityUser));
                }
                throw new CustomException(HttpStatusCode.Unauthorized);
            }