public async Task AddRequest(string Token, string Message, Guid AchievementId, CancellationToken Cancel)
        {
            var Claims = HelperService.GetClaimsFromToken(Token, Key);

            var UserId = new Guid(HelperService.GetIdFromToken(Claims));

            if (UserId == null)
            {
                throw new ArgumentNullException();
            }

            var Achievement = await UnitOfWork.AchievementRepository.GetById(AchievementId, Cancel);

            if (Achievement == null)
            {
                throw new ArgumentException();
            }

            var User = await UnitOfWork.UserRepository.GetById(UserId, Cancel);

            var Request = new RequestAchievement
            {
                Id            = new Guid(),
                UserId        = UserId,
                User          = User,
                AchievementId = AchievementId,
                Achievement   = Achievement,
                Message       = Message
            };
            await UnitOfWork.RequestAchievementRepository.Create(Request, Cancel);

            await UnitOfWork.SaveChangesAsync(Cancel);
        }
Exemplo n.º 2
0
        public async Task <ICollection <Achievement> > GetCurrentUserAchievements(string Token, CancellationToken Cancel)
        {
            var Claims = HelperService.GetClaimsFromToken(Token, Key);

            var UserId = HelperService.GetIdFromToken(Claims);

            if (UserId == null)
            {
                throw new ArgumentNullException();
            }

            var User = await UnitOfWork.UserRepository.DbSet
                       .Include(User => User.UserRoles)
                       .ThenInclude(UserRole => UserRole.Role)
                       .Include(User => User.UserAchievements)
                       .ThenInclude(UserAchievement => UserAchievement.Achievement)
                       .FirstOrDefaultAsync(User => User.Id == new Guid(UserId), Cancel);

            var Achievements = new List <Achievement>();

            foreach (var UserAchievement in User?.UserAchievements)
            {
                UserAchievement.Achievement.UserAchievements = null;
                Achievements.Add(UserAchievement.Achievement);
            }

            return(Achievements);
        }
Exemplo n.º 3
0
        public async Task <UserWithAchievementsDTO> GetCurrentUser(string Token, CancellationToken Cancel)
        {
            var Claims = HelperService.GetClaimsFromToken(Token, Key);
            var UserId = HelperService.GetIdFromToken(Claims);

            if (UserId == null)
            {
                throw new ArgumentNullException();
            }

            return(await UserService.GetUserById(new Guid(UserId), Cancel));
        }
Exemplo n.º 4
0
        public async Task <ThankReadDTO> GetThank(string Token, CancellationToken Cancel)
        {
            var Claims = HelperService.GetClaimsFromToken(Token, Key);
            var UserId = HelperService.GetIdFromToken(Claims);

            if (UserId == null)
            {
                new ArgumentNullException();
            }

            return(Mapper.Map <ThankReadDTO>(await UnitOfWork.ThankRepository.DbSet
                                             .Include(Thank => Thank.FromUser)
                                             .Where(Item => Item.ToUserId == new Guid(UserId))
                                             .OrderByDescending(Item => Item.AddedTime)
                                             .FirstOrDefaultAsync(Cancel)));
        }
Exemplo n.º 5
0
        public async Task ChangePassword(string Token, string OldPassword, string NewPassword, CancellationToken Cancel)
        {
            if (!ModelsValidator.RegExpPassword.IsMatch(NewPassword))
            {
                throw new ArgumentException();
            }

            var Claims = HelperService.GetClaimsFromToken(Token, Key);

            var UserId = HelperService.GetIdFromToken(Claims);

            if (UserId == null)
            {
                throw new ArgumentNullException();
            }

            var User = await UserService.GetUserById(new Guid(UserId), Cancel);

            string HashedOldPassword;
            string HashedNewPassword;

            using (SHA256 Sha256Hash = SHA256.Create())
            {
                var ByteArray = Sha256Hash.ComputeHash(Encoding.ASCII.GetBytes(OldPassword));
                HashedOldPassword = Encoding.UTF8.GetString(ByteArray, 0, ByteArray.Length);

                if (User.Password != HashedOldPassword)
                {
                    throw new ArgumentException();
                }

                if (OldPassword == NewPassword)
                {
                    throw new ArgumentException();
                }

                ByteArray         = Sha256Hash.ComputeHash(Encoding.ASCII.GetBytes(NewPassword));
                HashedNewPassword = Encoding.UTF8.GetString(ByteArray, 0, ByteArray.Length);

                var _User = await UnitOfWork.UserRepository.GetById(new Guid(UserId), Cancel);

                _User.Password = HashedNewPassword;

                UnitOfWork.UserRepository.Update(_User);
                await UnitOfWork.SaveChangesAsync(Cancel);
            }
        }
Exemplo n.º 6
0
        public async Task UpdateCurrentUser(string Token, UserUpdateDTO NewUser, CancellationToken Cancel)
        {
            if (!ModelsValidator.UserUpdateIsValid(NewUser))
            {
                throw new ArgumentException();
            }

            var Claims = HelperService.GetClaimsFromToken(Token, Key);

            var UserId = HelperService.GetIdFromToken(Claims);

            if (UserId == null)
            {
                throw new ArgumentNullException();
            }

            await UserService.UpdateUser(new Guid(UserId), NewUser, Cancel);

            await UnitOfWork.SaveChangesAsync(Cancel);
        }
        public async Task <(string, string, string)> RefreshToken(string Token, string RefreshToken, CancellationToken Cancel)
        {
            var Claims = HelperService.GetClaimsFromToken(Token, Key);

            if (Claims == null)
            {
                return(null, null, null);
            }

            var ExpiryDate = long.Parse(Claims.FirstOrDefault(Claim =>
                                                              Claim.Type == JwtRegisteredClaimNames.Exp).Value);

            var ExpiryDateTime = new DateTime(1970, 1, 1, 0, 0, 0)
                                 .AddSeconds(ExpiryDate);

            if (ExpiryDateTime > DateTime.UtcNow)
            {
                return(null, null, null);
            }

            var UserId = Claims.FirstOrDefault(Claim =>
                                               Claim.Type == JwtRegisteredClaimNames.Sub).Value;

            await DeleteUnUsedRefreshTokens(RefreshToken, UserId, Cancel);

            var StoredRefreshToken = await UnitOfWork.UserRepository.Context.RTokens.FirstOrDefaultAsync(Token => Token.Token == RefreshToken, Cancel);

            if (IsNotValidRefreshToken(StoredRefreshToken) || (StoredRefreshToken.UserId.ToString() != Claims.FirstOrDefault(Claim =>
                                                                                                                             Claim.Type == JwtRegisteredClaimNames.Sub).Value))
            {
                return(null, null, null);
            }

            UnitOfWork.UserRepository.Context.RTokens.Remove(StoredRefreshToken);
            await UnitOfWork.SaveChangesAsync(Cancel);

            var User = await UnitOfWork.UserRepository.GetById(StoredRefreshToken.UserId, Cancel);

            return(await Authenticate(User.UserName, User.Password, Cancel, true));
        }
Exemplo n.º 8
0
        public async Task SayThank(string Token, string Text, Guid ToUserId, CancellationToken Cancel)
        {
            var Claims = HelperService.GetClaimsFromToken(Token, Key);
            var UserId = HelperService.GetIdFromToken(Claims);

            if (UserId == null)
            {
                throw new ArgumentNullException();
            }

            var ThankBody = new Thank
            {
                FromUser  = await UnitOfWork.UserRepository.GetById(new Guid(UserId), Cancel),
                Text      = Text,
                ToUserId  = ToUserId,
                Id        = new Guid(),
                AddedTime = DateTime.Now.ToUniversalTime()
            };
            await UnitOfWork.ThankRepository.Create(ThankBody, Cancel);

            await UnitOfWork.SaveChangesAsync(Cancel);
        }
Exemplo n.º 9
0
 public EmployeeService()
 {
     helper = new HelperService();
 }
Exemplo n.º 10
0
 public ProductService()
 {
     helper = new HelperService();
 }