示例#1
0
        public void DesactivateUser(string userName)
        {
            Logger.LogInformation($"Try to desactivate user {userName}");

            using (var c = RepositoriesFactory.CreateContext())
            {
                var repo  = RepositoriesFactory.GetUserRepository(c);
                var user  = repo.GetByUserName(userName);
                var local = this.GetErrorStringLocalizer();

                if (user == null || !user.IsValid)
                {
                    throw new DaOAuthServiceException(local["DeleteUserNoUserFound"]);
                }

                user.IsValid = false;
                repo.Update(user);

                var ucRepo = RepositoriesFactory.GetUserClientRepository(c);

                foreach (var uc in ucRepo.GetAllByCriterias(user.UserName, null, null, null, 0, Int32.MaxValue))
                {
                    uc.RefreshToken = String.Empty;
                    ucRepo.Update(uc);
                }

                c.Commit();
            }
        }
示例#2
0
        private string GenerateAndSaveCode(string clientPublicId, string userName, string scope)
        {
            var codeValue = RandomService.GenerateRandomString(CodeLenght);

            using (var context = RepositoriesFactory.CreateContext())
            {
                var userClientRepo = RepositoriesFactory.GetUserClientRepository(context);
                var codeRepo       = RepositoriesFactory.GetCodeRepository(context);

                var uc = userClientRepo.GetUserClientByClientPublicIdAndUserName(clientPublicId, userName);

                codeRepo.Add(new Domain.Code()
                {
                    CodeValue           = codeValue,
                    ExpirationTimeStamp = new DateTimeOffset(DateTime.Now.AddSeconds(Configuration.CodeDurationInSeconds)).ToUnixTimeSeconds(),
                    IsValid             = true,
                    Scope        = scope,
                    UserClientId = uc.Id
                });

                context.Commit();
            }

            return(codeValue);
        }
示例#3
0
        public void DeleteReturnUrl(DeleteReturnUrlDto toDelete)
        {
            this.Validate(toDelete);

            var resource = this.GetErrorStringLocalizer();

            using (var context = RepositoriesFactory.CreateContext())
            {
                var returnUrlRepo = RepositoriesFactory.GetClientReturnUrlRepository(context);
                var myReturnUrl   = returnUrlRepo.GetById(toDelete.IdReturnUrl);

                if (myReturnUrl == null)
                {
                    throw new DaOAuthServiceException(resource["DeleteReturnUrlUnknowReturnUrl"]);
                }

                var userRepo = RepositoriesFactory.GetUserRepository(context);
                var user     = userRepo.GetByUserName(toDelete.UserName);
                if (user == null || !user.IsValid)
                {
                    throw new DaOAuthServiceException(resource["DeleteReturnUrlInvalidUser"]);
                }

                var ucRepo = RepositoriesFactory.GetUserClientRepository(context);
                var uc     = ucRepo.GetUserClientByClientPublicIdAndUserName(myReturnUrl.Client.PublicId, toDelete.UserName);
                if (uc == null)
                {
                    throw new DaOAuthServiceException(resource["DeleteReturnUrlBadUserNameOrClientId"]);
                }

                returnUrlRepo.Delete(myReturnUrl);

                context.Commit();
            }
        }
示例#4
0
        public void SendMailLostPassword(LostPasswordDto infos)
        {
            Logger.LogInformation($"Send a mail password lost to {infos.Email}");

            Validate(infos);

            using (var context = RepositoriesFactory.CreateContext())
            {
                var errorResource = this.GetErrorStringLocalizer();
                var mailResource  = this.GetMailStringLocalizer();
                var userRepo      = RepositoriesFactory.GetUserRepository(context);

                var user = userRepo.GetByEmail(infos.Email);

                if (user == null || !user.IsValid)
                {
                    throw new DaOAuthServiceException(errorResource["SendMailLostPasswordUserNoUserFound"]);
                }

                var myToken = JwtService.GenerateMailToken(user.UserName);
                var link    = new Uri(String.Format(Configuration.GetNewPasswordPageUrl, myToken.Token));

                MailService.SendEmail(new SendEmailDto()
                {
                    Body      = String.Format(mailResource["MailLostPasswordBody"], link.AbsoluteUri),
                    IsHtml    = true,
                    Receviers = new Dictionary <string, string>()
                    {
                        { user.EMail, user.EMail }
                    },
                    Sender  = new KeyValuePair <string, string>("*****@*****.**", "no reply"),
                    Subject = mailResource["MailLostPasswordSubject"]
                });
            }
        }
示例#5
0
        public ClientDto GetById(int id, string userName)
        {
            Logger.LogInformation(String.Format("Try to get client by id {0}", id));

            ClientDto toReturn = null;

            using (var context = RepositoriesFactory.CreateContext())
            {
                var clientRepo = RepositoriesFactory.GetClientRepository(context);

                var client = clientRepo.GetById(id);

                if (client == null || !client.IsValid)
                {
                    throw new DaOAuthNotFoundException();
                }

                // we need to remove scopes from invalid ressources servers
                if (client.ClientsScopes != null && client.ClientsScopes.Count() > 0)
                {
                    var invalidsRs = ExtractInvalidRessourceServerIds(context);

                    client.ClientsScopes = client.ClientsScopes.ToList().Where(cs => !invalidsRs.Contains(cs.Scope.RessourceServerId)).ToList();
                }

                toReturn = client.ToDto(client.UserCreator.UserName.Equals(userName));
            }

            return(toReturn);
        }
示例#6
0
 private bool CheckIfUserHasAuthorizeOrDeniedClientAccess(string clientPublicId, string userName)
 {
     using (var context = RepositoriesFactory.CreateContext())
     {
         var clientUserRepo = RepositoriesFactory.GetUserClientRepository(context);
         return(clientUserRepo.GetUserClientByClientPublicIdAndUserName(clientPublicId, userName) != null);
     }
 }
示例#7
0
        private TokenInfoDto GenerateTokenForRefreshToken(AskTokenDto tokenInfo, IStringLocalizer errorLocal)
        {
            TokenInfoDto toReturn = null;

            if (String.IsNullOrWhiteSpace(tokenInfo.ClientPublicId))
            {
                throw new DaOAuthTokenException()
                      {
                          Error       = OAuthConvention.ErrorNameRefreshToken,
                          Description = errorLocal["RefreshTokenParameterError"]
                      };
            }

            using (var context = RepositoriesFactory.CreateContext())
            {
                var clientRepo = RepositoriesFactory.GetClientRepository(context);
                var myClient   = clientRepo.GetByPublicId(tokenInfo.ClientPublicId);

                if (!CheckIfClientsCredentialsAreValid(myClient, tokenInfo.AuthorizationHeader))
                {
                    throw new DaOAuthTokenException()
                          {
                              Error       = OAuthConvention.ErrorNameUnauthorizedClient,
                              Description = errorLocal["UnauthorizedClient"]
                          };
                }

                var tokenDetail = JwtService.ExtractToken(new ExtractTokenDto()
                {
                    Token     = tokenInfo.RefreshToken,
                    TokenName = OAuthConvention.RefreshToken
                });

                if (!CheckIfTokenIsValid(tokenDetail, context))
                {
                    throw new DaOAuthTokenException()
                          {
                              Error       = OAuthConvention.ErrorNameInvalidGrant,
                              Description = errorLocal["RefreshTokenInvalid"]
                          };
                }

                if (!CheckIfScopesAreAuthorizedForClient(tokenDetail.ClientId, tokenInfo.Scope))
                {
                    throw new DaOAuthTokenException()
                          {
                              Error       = OAuthConvention.ErrorNameInvalidScope,
                              Description = errorLocal["UnauthorizedScope"]
                          };
                }

                toReturn = GenerateAccessTokenAndUpdateRefreshToken(tokenInfo, context, tokenDetail.UserName);

                context.Commit();
            }

            return(toReturn);
        }
示例#8
0
 private bool CheckIfUserHasAuthorizeClient(string clientPublicId, string userName)
 {
     using (var context = RepositoriesFactory.CreateContext())
     {
         var clientUserRepo = RepositoriesFactory.GetUserClientRepository(context);
         var uc             = clientUserRepo.GetUserClientByClientPublicIdAndUserName(clientPublicId, userName);
         return(uc != null && uc.IsActif);
     }
 }
示例#9
0
        public int CreateReturnUrl(CreateReturnUrlDto toCreate)
        {
            this.Validate(toCreate);

            var idCreated = 0;

            var resource = this.GetErrorStringLocalizer();

            if (!Uri.TryCreate(toCreate.ReturnUrl, UriKind.Absolute, out var u))
            {
                throw new DaOAuthServiceException(resource["CreateReturnUrlReturnUrlIncorrect"]);
            }

            using (var context = RepositoriesFactory.CreateContext())
            {
                var userRepo = RepositoriesFactory.GetUserRepository(context);
                var user     = userRepo.GetByUserName(toCreate.UserName);
                if (user == null || !user.IsValid)
                {
                    throw new DaOAuthServiceException(resource["CreateReturnUrlInvalidUser"]);
                }

                var ucRepo = RepositoriesFactory.GetUserClientRepository(context);
                var uc     = ucRepo.GetUserClientByClientPublicIdAndUserName(toCreate.ClientPublicId, toCreate.UserName);
                if (uc == null)
                {
                    throw new DaOAuthServiceException(resource["CreateReturnUrlBadUserNameOrClientId"]);
                }

                var clientRepo = RepositoriesFactory.GetClientRepository(context);
                var client     = clientRepo.GetByPublicId(toCreate.ClientPublicId);
                if (client == null || !client.IsValid)
                {
                    throw new DaOAuthServiceException(resource["CreateReturnUrlInvalidClient"]);
                }

                var existingReturnUrl = client.ClientReturnUrls.FirstOrDefault(c => c.ReturnUrl.Equals(toCreate.ReturnUrl, StringComparison.OrdinalIgnoreCase));

                if (existingReturnUrl != null)
                {
                    idCreated = existingReturnUrl.Id;
                }
                else
                {
                    var returnUrlRepo = RepositoriesFactory.GetClientReturnUrlRepository(context);
                    idCreated = returnUrlRepo.Add(new Domain.ClientReturnUrl()
                    {
                        ClientId  = client.Id,
                        ReturnUrl = toCreate.ReturnUrl
                    });
                }

                context.Commit();
            }

            return(idCreated);
        }
示例#10
0
        public void Delete(DeleteClientDto toDelete)
        {
            Logger.LogInformation(String.Format("Try to create delete by user {0}", toDelete != null ? toDelete.UserName : String.Empty));

            Validate(toDelete);

            using (var context = RepositoriesFactory.CreateContext())
            {
                var clientRepo          = RepositoriesFactory.GetClientRepository(context);
                var userClientRepo      = RepositoriesFactory.GetUserClientRepository(context);
                var userRepo            = RepositoriesFactory.GetUserRepository(context);
                var clientScopeRepo     = RepositoriesFactory.GetClientScopeRepository(context);
                var clientReturnUrlRepo = RepositoriesFactory.GetClientReturnUrlRepository(context);

                var myUser = userRepo.GetByUserName(toDelete.UserName);

                if (myUser == null || !myUser.IsValid)
                {
                    throw new DaOAuthServiceException("DeleteClientInvalidUserName");
                }

                var myClient = clientRepo.GetById(toDelete.Id);

                if (myClient == null)
                {
                    throw new DaOAuthServiceException("DeleteClientUnknowClient");
                }

                var myUserClient = userClientRepo
                                   .GetUserClientByClientPublicIdAndUserName(myClient.PublicId, toDelete.UserName);

                if (myUserClient == null || !myUserClient.Client.UserCreator.UserName.Equals(toDelete.UserName, StringComparison.OrdinalIgnoreCase))
                {
                    throw new DaOAuthServiceException("DeleteClientWrongUser");
                }

                foreach (var cr in clientReturnUrlRepo.GetAllByClientPublicId(myClient.PublicId).ToList())
                {
                    clientReturnUrlRepo.Delete(cr);
                }

                foreach (var cs in clientScopeRepo.GetAllByClientId(myClient.Id).ToList())
                {
                    clientScopeRepo.Delete(cs);
                }

                foreach (var uc in userClientRepo.GetAllByClientId(myClient.Id).ToList())
                {
                    userClientRepo.Delete(uc);
                }

                userClientRepo.Delete(myUserClient);
                clientRepo.Delete(myClient);

                context.Commit();
            }
        }
示例#11
0
        public int SearchCount(AdminUserSearchDto criterias)
        {
            Validate(criterias, ExtendValidationSearchCriterias);

            using (var c = RepositoriesFactory.CreateContext())
            {
                var userRepo = RepositoriesFactory.GetUserRepository(c);
                return(userRepo.GetAllByCriteriasCount(criterias.UserName, criterias.Email, criterias.IsValid));
            }
        }
示例#12
0
        public int SearchCount(RessourceServerSearchDto criterias)
        {
            Validate(criterias, ExtendValidationSearchCriterias);

            var count = 0;

            using (var c = RepositoriesFactory.CreateContext())
            {
                var rsRepo = RepositoriesFactory.GetRessourceServerRepository(c);
                count = rsRepo.GetAllByCriteriasCount(criterias.Name, criterias.Login, true);
            }

            return(count);
        }
示例#13
0
        public int SearchCount(UserClientSearchDto criterias)
        {
            Validate(criterias, ExtendValidationSearchCriterias);

            var count = 0;

            using (var c = RepositoriesFactory.CreateContext())
            {
                var userClientRepo = RepositoriesFactory.GetUserClientRepository(c);
                count = userClientRepo.GetAllByCriteriasCount(criterias.UserName, criterias.Name, true, GetClientTypeId(criterias.ClientType));
            }

            return(count);
        }
示例#14
0
        public IEnumerable <ScopeDto> GetAll()
        {
            using (var context = RepositoriesFactory.CreateContext())
            {
                var scopes    = new List <Scope>();
                var rsRepo    = RepositoriesFactory.GetRessourceServerRepository(context);
                var scopeRepo = RepositoriesFactory.GetScopeRepository(context);

                var validRs = rsRepo.GetAll().Where(rs => rs.IsValid.Equals(true)).Select(rs => rs.Id).ToList();

                scopes = scopeRepo.GetAll().Where(s => validRs.Contains(s.RessourceServerId)).ToList();

                return(scopes.ToDto());
            }
        }
示例#15
0
        public void DeleteUser(string userName)
        {
            Logger.LogInformation($"delete user {userName}");

            using (var context = RepositoriesFactory.CreateContext())
            {
                var userRepository       = RepositoriesFactory.GetUserRepository(context);
                var clientRepository     = RepositoriesFactory.GetClientRepository(context);
                var userClientRepository = RepositoriesFactory.GetUserClientRepository(context);

                var user  = userRepository.GetByUserName(userName);
                var local = this.GetErrorStringLocalizer();

                if (user == null || !user.IsValid)
                {
                    throw new DaOAuthServiceException(local["DeleteUserNoUserFound"]);
                }

                var clients = clientRepository.GetAllClientsByIdCreator(user.Id);

                IList <Client>     toDeleteClients      = new List <Client>();
                IList <UserClient> toDeleteUsersClients = new List <UserClient>();

                foreach (var client in clients)
                {
                    foreach (var uc in userClientRepository.GetAllByClientId(client.Id))
                    {
                        toDeleteUsersClients.Add(uc);
                    }

                    toDeleteClients.Add(client);
                }

                foreach (var uc in toDeleteUsersClients)
                {
                    userClientRepository.Delete(uc);
                }

                foreach (var c in toDeleteClients)
                {
                    clientRepository.Delete(c);
                }

                userRepository.Delete(user);

                context.Commit();
            }
        }
示例#16
0
        public AdminUserDetailDto GetByIdUser(int idUser)
        {
            using (var context = RepositoriesFactory.CreateContext())
            {
                var userClientRepo = RepositoriesFactory.GetUserRepository(context);

                var user = userClientRepo.GetById(idUser);

                if (user == null)
                {
                    throw new DaOAuthNotFoundException("GetByIdUserUserNotFound");
                }

                return(user.ToAdminDetailDto());
            }
        }
示例#17
0
        private TokenInfoDto GenerateTokenForClientCredentailsGrant(AskTokenDto tokenInfo, IStringLocalizer errorLocal)
        {
            TokenInfoDto toReturn = null;

            using (var context = RepositoriesFactory.CreateContext())
            {
                var clientRepo = RepositoriesFactory.GetClientRepository(context);
                var myClient   = clientRepo.GetByPublicId(tokenInfo.ClientPublicId);

                if (!CheckIfClientsCredentialsAreValid(myClient, tokenInfo.AuthorizationHeader))
                {
                    throw new DaOAuthTokenException()
                          {
                              Error       = OAuthConvention.ErrorNameUnauthorizedClient,
                              Description = errorLocal["UnauthorizedClient"]
                          };
                }

                if (!CheckIfScopesAreAuthorizedForClient(myClient.PublicId, tokenInfo.Scope))
                {
                    throw new DaOAuthTokenException()
                          {
                              Error       = OAuthConvention.ErrorNameInvalidScope,
                              Description = errorLocal["UnauthorizedScope"]
                          };
                }

                var accesToken = JwtService.GenerateToken(new CreateTokenDto()
                {
                    ClientPublicId  = myClient.PublicId,
                    Scope           = tokenInfo.Scope,
                    SecondsLifeTime = Configuration.AccesTokenLifeTimeInSeconds,
                    TokenName       = OAuthConvention.AccessToken,
                    UserName        = String.Empty
                });

                toReturn = new TokenInfoDto()
                {
                    AccessToken = accesToken.Token,
                    ExpireIn    = Configuration.AccesTokenLifeTimeInSeconds,
                    Scope       = tokenInfo.Scope,
                    TokenType   = OAuthConvention.AccessToken
                };
            }

            return(toReturn);
        }
示例#18
0
        public void UpdateUser(UpdateUserDto toUpdate)
        {
            IList <ValidationResult> ExtendValidation(UpdateUserDto toValidate)
            {
                var resource = this.GetErrorStringLocalizer();
                IList <ValidationResult> result = new List <ValidationResult>();

                using (var c = RepositoriesFactory.CreateContext())
                {
                    var repo = RepositoriesFactory.GetUserRepository(c);
                    var user = repo.GetByUserName(toUpdate.UserName);

                    if (user == null || !user.IsValid)
                    {
                        result.Add(new ValidationResult(resource["UpdateUserNoUserFound"]));
                    }

                    var errorResource = this.GetErrorStringLocalizer();
                    var getByMailUser = repo.GetByEmail(toValidate.EMail);
                    if (getByMailUser != null && getByMailUser.Id != user.Id)
                    {
                        result.Add(new ValidationResult(String.Format(errorResource["CreateUserEmailExists"], toValidate.EMail)));
                    }
                }

                return(result);
            }

            Logger.LogInformation(String.Format("Try to update user {0}", toUpdate != null ? toUpdate.UserName : String.Empty));

            Validate(toUpdate, ExtendValidation);

            using (var c = RepositoriesFactory.CreateContext())
            {
                var repo = RepositoriesFactory.GetUserRepository(c);
                var user = repo.GetByUserName(toUpdate.UserName);

                user.BirthDate = toUpdate.BirthDate;
                user.EMail     = toUpdate.EMail;
                user.FullName  = toUpdate.FullName;

                repo.Update(user);

                c.Commit();
            }
        }
示例#19
0
        public int CreateUserClient(CreateUserClientDto toCreate)
        {
            Validate(toCreate);

            var id = 0;

            var local = GetErrorStringLocalizer();

            using (var context = RepositoriesFactory.CreateContext())
            {
                var userClientRepo = RepositoriesFactory.GetUserClientRepository(context);
                var clientRepo     = RepositoriesFactory.GetClientRepository(context);
                var userRepo       = RepositoriesFactory.GetUserRepository(context);

                var user = userRepo.GetByUserName(toCreate.UserName);
                if (user == null || !user.IsValid)
                {
                    throw new DaOAuthServiceException(local["CreateUserClientInvalidUserName"]);
                }

                var client = clientRepo.GetByPublicId(toCreate.ClientPublicId);
                if (client == null || !client.IsValid)
                {
                    throw new DaOAuthServiceException(local["CreateUserClientInvalidClientPublicId"]);
                }

                var uc = userClientRepo.GetUserClientByClientPublicIdAndUserName(toCreate.ClientPublicId, toCreate.UserName);
                if (uc != null)
                {
                    throw new DaOAuthServiceException(local["CreateUserClientClientAlreadyRegister"]);
                }

                id = userClientRepo.Add(new UserClient()
                {
                    ClientId     = client.Id,
                    CreationDate = DateTime.Now,
                    IsActif      = toCreate.IsActif,
                    UserId       = user.Id
                });

                context.Commit();
            }

            return(id);
        }
示例#20
0
        public IEnumerable <UserClientListDto> Search(UserClientSearchDto criterias)
        {
            Validate(criterias, ExtendValidationSearchCriterias);

            IList <UserClient> clients = null;

            var clientTypeId = GetClientTypeId(criterias.ClientType);

            using (var context = RepositoriesFactory.CreateContext())
            {
                var clientRepo = RepositoriesFactory.GetUserClientRepository(context);

                clients = clientRepo.GetAllByCriterias(criterias.UserName, criterias.Name,
                                                       true, clientTypeId, criterias.Skip, criterias.Limit).ToList();
            }

            return(clients != null?clients.ToDto(criterias.UserName) : new List <UserClientListDto>());
        }
示例#21
0
        public IEnumerable <RessourceServerDto> Search(RessourceServerSearchDto criterias)
        {
            Logger.LogInformation("Search ressource servers");

            Validate(criterias, ExtendValidationSearchCriterias);

            IList <RessourceServer> rs = null;


            using (var context = RepositoriesFactory.CreateContext())
            {
                var rsRepo = RepositoriesFactory.GetRessourceServerRepository(context);

                rs = rsRepo.GetAllByCriterias(criterias.Name, criterias.Login, true, criterias.Skip, criterias.Limit).ToList();
            }

            return(rs != null?rs.ToDto() : new List <RessourceServerDto>());
        }
示例#22
0
        public void Delete(DeleteRessourceServerDto toDelete)
        {
            Logger.LogInformation(String.Format("Try to delete ressource server for user {0}", toDelete != null ? toDelete.UserName : String.Empty));

            Validate(toDelete);

            using (var context = RepositoriesFactory.CreateContext())
            {
                var userRepo        = RepositoriesFactory.GetUserRepository(context);
                var rsRepo          = RepositoriesFactory.GetRessourceServerRepository(context);
                var scopeRepo       = RepositoriesFactory.GetScopeRepository(context);
                var clientScopeRepo = RepositoriesFactory.GetClientScopeRepository(context);

                var myUser = userRepo.GetByUserName(toDelete.UserName);
                if (myUser == null || !myUser.IsValid)
                {
                    throw new DaOAuthServiceException("DeleteRessourceServerInvalidUserName");
                }
                if (myUser.UsersRoles.FirstOrDefault(r => r.RoleId.Equals((int)ERole.ADMIN)) == null)
                {
                    throw new DaOAuthServiceException("DeleteRessourceServerNonAdminUserName");
                }

                var myRs = rsRepo.GetById(toDelete.Id);
                if (myRs == null)
                {
                    throw new DaOAuthServiceException("DeleteRessourceServerRessourceServerNotFound");
                }

                foreach (var s in myRs.Scopes.ToList())
                {
                    foreach (var cs in clientScopeRepo.GetAllByScopeId(s.Id).ToList())
                    {
                        clientScopeRepo.Delete(cs);
                    }
                    scopeRepo.Delete(s);
                }

                rsRepo.Delete(myRs);

                context.Commit();
            }
        }
示例#23
0
        public UserDto GetUser(string userName)
        {
            Logger.LogInformation($"Try get user {userName}");

            UserDto result = null;

            using (var c = RepositoriesFactory.CreateContext())
            {
                var repo = RepositoriesFactory.GetUserRepository(c);
                var user = repo.GetByUserName(userName);

                if (user != null && user.IsValid)
                {
                    result = user.ToDto();
                }
            }

            return(result);
        }
示例#24
0
        public void UpdateReturnUrl(UpdateReturnUrlDto toUpdate)
        {
            this.Validate(toUpdate);

            var resource = this.GetErrorStringLocalizer();

            if (!Uri.TryCreate(toUpdate.ReturnUrl, UriKind.Absolute, out var u))
            {
                throw new DaOAuthServiceException(resource["UpdateReturnUrlReturnUrlIncorrect"]);
            }

            using (var context = RepositoriesFactory.CreateContext())
            {
                var returnUrlRepo = RepositoriesFactory.GetClientReturnUrlRepository(context);
                var myReturnUrl   = returnUrlRepo.GetById(toUpdate.IdReturnUrl);

                if (myReturnUrl == null)
                {
                    throw new DaOAuthServiceException(resource["UpdateReturnUrlUnknowReturnUrl"]);
                }

                var userRepo = RepositoriesFactory.GetUserRepository(context);
                var user     = userRepo.GetByUserName(toUpdate.UserName);
                if (user == null || !user.IsValid)
                {
                    throw new DaOAuthServiceException(resource["UpdateReturnUrlInvalidUser"]);
                }

                var ucRepo = RepositoriesFactory.GetUserClientRepository(context);
                var uc     = ucRepo.GetUserClientByClientPublicIdAndUserName(myReturnUrl.Client.PublicId, toUpdate.UserName);
                if (uc == null)
                {
                    throw new DaOAuthServiceException(resource["UpdateReturnUrlBadUserNameOrClientId"]);
                }

                myReturnUrl.ReturnUrl = toUpdate.ReturnUrl;

                returnUrlRepo.Update(myReturnUrl);

                context.Commit();
            }
        }
示例#25
0
        public void UpdateUserClient(UpdateUserClientDto toUpdate)
        {
            IList <ValidationResult> ExtendValidation(UpdateUserClientDto toValidate)
            {
                var resource = this.GetErrorStringLocalizer();
                IList <ValidationResult> result = new List <ValidationResult>();

                using (var context = RepositoriesFactory.CreateContext())
                {
                    var ucRepo = RepositoriesFactory.GetUserClientRepository(context);
                    var myUc   = ucRepo.GetUserClientByClientPublicIdAndUserName(toValidate.ClientPublicId, toValidate.UserName);

                    if (myUc == null)
                    {
                        result.Add(new ValidationResult(resource["UpdateUserClientUserOrClientNotFound"]));
                    }

                    if (myUc != null && (myUc.User == null || !myUc.User.IsValid))
                    {
                        result.Add(new ValidationResult(resource["UpdateUserClientUserNotValid"]));
                    }

                    if (myUc != null && (myUc.Client == null || !myUc.Client.IsValid))
                    {
                        result.Add(new ValidationResult(resource["UpdateUserClientClientNotValid"]));
                    }
                }

                return(result);
            }

            this.Validate(toUpdate, ExtendValidation);

            using (var context = RepositoriesFactory.CreateContext())
            {
                var ucRepo = RepositoriesFactory.GetUserClientRepository(context);
                var myUc   = ucRepo.GetUserClientByClientPublicIdAndUserName(toUpdate.ClientPublicId, toUpdate.UserName);
                myUc.IsActif = toUpdate.IsActif;
                ucRepo.Update(myUc);
                context.Commit();
            }
        }
示例#26
0
        public void ChangeUserPassword(ChangePasswordDto infos)
        {
            Logger.LogInformation($"Change password from user {infos.UserName}");

            Validate(infos);

            using (var context = RepositoriesFactory.CreateContext())
            {
                var local    = this.GetErrorStringLocalizer();
                var userRepo = RepositoriesFactory.GetUserRepository(context);

                var user = userRepo.GetByUserName(infos.UserName);

                if (user == null || !user.IsValid)
                {
                    throw new DaOAuthServiceException(local["ChangeUserPasswordUserInvalid"]);
                }

                if (!EncryptionService.AreEqualsSha256(
                        String.Concat(Configuration.PasswordSalt, infos.OldPassword), user.Password))
                {
                    throw new DaOAuthServiceException(local["ChangeUserPasswordPasswordInvalid"]);
                }

                if (!infos.NewPassword.Equals(infos.NewPasswordRepeat, StringComparison.Ordinal))
                {
                    throw new DaOAuthServiceException(local["ChangeUserPasswordDifferentsNewPasswords"]);
                }

                if (!infos.NewPassword.IsMatchPasswordPolicy())
                {
                    throw new DaOAuthServiceException(local["ChangeUserPasswordNewPasswordDontMatchPolicy"]);
                }

                user.Password = EncryptionService.Sha256Hash(String.Concat(Configuration.PasswordSalt, infos.NewPassword));

                userRepo.Update(user);

                context.Commit();
            }
        }
示例#27
0
        public IEnumerable <AdminUsrDto> Search(AdminUserSearchDto criterias)
        {
            Validate(criterias, ExtendValidationSearchCriterias);

            IList <User> users = null;

            using (var context = RepositoriesFactory.CreateContext())
            {
                var userRepo = RepositoriesFactory.GetUserRepository(context);

                users = userRepo.GetAllByCriterias(criterias.UserName, criterias.Email, criterias.IsValid,
                                                   criterias.Skip, criterias.Limit).ToList();
            }

            if (users != null)
            {
                return(users.ToAdminDto());
            }

            return(new List <AdminUsrDto>());
        }
示例#28
0
        public void ActivateUser(string userName)
        {
            Logger.LogInformation($"Try to activate user {userName}");

            using (var c = RepositoriesFactory.CreateContext())
            {
                var repo  = RepositoriesFactory.GetUserRepository(c);
                var user  = repo.GetByUserName(userName);
                var local = this.GetErrorStringLocalizer();

                if (user == null || user.IsValid)
                {
                    throw new DaOAuthServiceException(local["DeleteUserNoUserFound"]);
                }

                user.IsValid = true;
                repo.Update(user);

                c.Commit();
            }
        }
示例#29
0
        public RessourceServerDto GetById(int id)
        {
            Logger.LogInformation(String.Format("Try to get ressource server by id {0}", id));

            RessourceServerDto toReturn = null;

            using (var context = RepositoriesFactory.CreateContext())
            {
                var rsRepo = RepositoriesFactory.GetRessourceServerRepository(context);
                var rs     = rsRepo.GetById(id);

                if (rs == null || !rs.IsValid)
                {
                    throw new DaOAuthNotFoundException();
                }

                toReturn = rs.ToDto();
            }

            return(toReturn);
        }
示例#30
0
        private IList <ValidationResult> ExtendValidationSearchCriterias(UserClientSearchDto c)
        {
            var resource = this.GetErrorStringLocalizer();
            IList <ValidationResult> result = new List <ValidationResult>();

            using (var context = RepositoriesFactory.CreateContext())
            {
                var userRepo = RepositoriesFactory.GetUserRepository(context);
                var user     = userRepo.GetByUserName(c.UserName);
                if (user == null || !user.IsValid)
                {
                    result.Add(new ValidationResult(String.Format(resource["SearchClientInvalidUser"], c)));
                }
            }

            if (c.Limit - c.Skip > 50)
            {
                result.Add(new ValidationResult(String.Format(resource["SearchClientAskTooMuch"], c)));
            }

            return(result);
        }