public async Task Login(LoginViewModel model)
        {
            var user = await _userRepo.GetUserByEmailAsync(model.Email);

            if (user == null)
            {
                throw new Exception("Użytkownik nie istnieje");
            }

            var userInRoles = user.UserInRoles;
            var roles       = new List <Role>();

            foreach (var item in userInRoles)
            {
                var role = await _roleRepo.GetRoleByIdAsync(item.RoleId);

                roles.Add(role);
            }

            var hash      = _encrypter.GetHash(model.Password, user.Salt);
            var tokenTemp = _jwtHandler.CreateToken(user.UserId.ToString(), roles);

            if (user.Password == hash)
            {
                _cache.SetJwt(model.TokenId, tokenTemp);
            }
        }
Пример #2
0
        public async Task <IEnumerable <UserViewModel> > GetUsersAsync()
        {
            var users = await _userRepo.GetUsersAsync();

            var uservmList = new List <UserViewModel>();

            foreach (var item in users)
            {
                var userInRoles = item.UserInRoles;
                var rolevmList  = new List <RoleViewModel>();

                foreach (var userInRole in item.UserInRoles)
                {
                    var role = await _roleRepo.GetRoleByIdAsync(userInRole.RoleId);

                    rolevmList.Add(_mapper.Map <RoleViewModel>(role));
                }

                var uservm = new UserViewModel()
                {
                    UserId    = item.UserId,
                    Email     = item.Email,
                    UserName  = item.UserName,
                    Roles     = rolevmList,
                    CreatedAt = item.CreateAt,
                    UpdatetAt = item.UpdateAt
                };

                uservmList.Add(uservm);
            }

            return(uservmList);
        }
Пример #3
0
        public async Task <RoleViewModel> GetRoleByIdAsync(Guid roleId)
        {
            var user = await _roleRepo.GetRoleByIdAsync(roleId);

            return(_mapper.Map <RoleViewModel>(user));
        }