public RegistrationDataResponse Authorize() { String login = ConfigurationManager.AppSettings["login"]; String password = ConfigurationManager.AppSettings["password"]; RegistrationContent registration = new RegistrationContent() {Login = login, Password = password}; var response = _httpClient.PostAsync(_baseUrl + _authorizationUrl, registration, new JsonMediaTypeFormatter()).Result; AuthorizeResponseContent content = response.Content.ReadAsAsync<AuthorizeResponseContent>().Result; if (content.Status) { RegistrationDataResponse registratedClient = JsonHelper.JsonDeserialize<RegistrationDataResponse>(content.Data.ToString()); return registratedClient; } return null; }
public void Update(long userId, RegistrationContent updatedUser) { using(var clientRepository = new BaseRepository<Client>()) { Client foundedClient = clientRepository.GetAllItems.FirstOrDefault( x => x.Id == userId); if (foundedClient == null) { throw new UserDoesNotExistException(); } Person personRelated = clientRepository.Context.Persons.FirstOrDefault(x => x.Id == foundedClient.PersonId); if (personRelated == null) { throw new UserDoesNotExistException(); } // Login updated if (!String.IsNullOrWhiteSpace(updatedUser.Login)) { // Check Login Unique if(!String.Equals(updatedUser.Login, foundedClient.Login)) { if (clientRepository.GetAllItems.Any(x => String.Equals(x.Login, updatedUser.Login))) { throw new UserLoginException(); } foundedClient.Login = updatedUser.Login; } } // Email updated if (!String.IsNullOrWhiteSpace(updatedUser.Email)) { // Check Login Unique if (!String.Equals(updatedUser.Email, personRelated.Email)) { if (clientRepository.Context.Persons.Any(x => String.Equals(x.Email, updatedUser.Email))) { throw new UserEmailException(); } personRelated.Email = updatedUser.Email; } } // Password updated if(!String.IsNullOrWhiteSpace(updatedUser.Password)) { // Check password validation if(updatedUser.Password.Length < 6) { throw new UserPasswordException(); } foundedClient.Password = updatedUser.Password; } // Other fields updated if(!String.IsNullOrWhiteSpace(updatedUser.FirstName)) { personRelated.FirstName = updatedUser.FirstName; } if (!String.IsNullOrWhiteSpace(updatedUser.LastName)) { personRelated.LastName = updatedUser.LastName; } if (!String.IsNullOrWhiteSpace(updatedUser.MiddleName)) { personRelated.MiddleName = updatedUser.MiddleName; } if(updatedUser.BirthDate.HasValue) { personRelated.BirthDate = updatedUser.BirthDate.Value; } if(updatedUser.Gender.HasValue) { personRelated.Gender = updatedUser.Gender.Value; } if (updatedUser.IsCurator.HasValue) { foundedClient.IsCurator = updatedUser.IsCurator.Value; } // is it need? if (updatedUser.IsAdmin.HasValue) { foundedClient.IsAdmin = updatedUser.IsAdmin.Value; } // update clientRepository.Context.Entry(personRelated).State = EntityState.Modified; clientRepository.Context.SaveChanges(); if (!clientRepository.Update(foundedClient).Status) { throw new ConsulExceptions.UpdateException(); } } }
public RegistrationContent Authorize(String login, String password) { using (var clientRepository = new BaseRepository<Client>()) { Client foundedClient = clientRepository.GetAllItems.FirstOrDefault( x => String.Equals(x.Login, login) && String.Equals(x.Password, password)); if (foundedClient == null) { throw new UserDoesNotExistException(); } Person personRelated = clientRepository.Context.Persons.FirstOrDefault(x => x.Id == foundedClient.PersonId); if (personRelated == null) { throw new UserDoesNotExistException(); } Authorization authorization = clientRepository.Context.Authorizations.FirstOrDefault(x => x.ClientId == foundedClient.Id); if (authorization != null) { // Update row in Authorizations if (authorization.ExpirationDate < DateTime.Now) { authorization.Token = Guid.NewGuid(); } authorization.ExpirationDate = DateTime.Now.AddMinutes(Constraints.KExpirationMinutes); clientRepository.Context.Entry(authorization).State = EntityState.Modified; } else { // Add row in Authorizations authorization = new Authorization() { ClientId = foundedClient.Id, ExpirationDate = DateTime.Now.AddMinutes(Constraints.KExpirationMinutes), Token = Guid.NewGuid() }; clientRepository.Context.Authorizations.Add(authorization); } clientRepository.Context.SaveChanges(); // Form output registration content RegistrationContent content = new RegistrationContent(); content.Id = foundedClient.Id; content.Login = foundedClient.Login; content.Password = foundedClient.Password; content.IsCurator = foundedClient.IsCurator; content.IsAdmin = foundedClient.IsAdmin; content.PersonId = personRelated.Id; content.FirstName = personRelated.FirstName; content.LastName = personRelated.LastName; content.MiddleName = personRelated.MiddleName; content.Gender = personRelated.Gender; content.BirthDate = personRelated.BirthDate; content.Email = personRelated.Email; content.Token = authorization.Token; content.ExpirationDate = authorization.ExpirationDate; return content; } }
public RegistrationContent Register(RegistrationContent registratedUser) { using (var clientRepository = new BaseRepository<Client>()) { // Check required fields if (String.IsNullOrWhiteSpace(registratedUser.Login) || String.IsNullOrWhiteSpace(registratedUser.Email) || !registratedUser.BirthDate.HasValue || String.IsNullOrWhiteSpace(registratedUser.Password) || !registratedUser.Gender.HasValue) { throw new RequireFieldException(); } // Check password validation if(registratedUser.Password.Length < 6) { throw new UserPasswordException(); } // Check Login unique // SELECT Login from Clients if (clientRepository.GetAllItems.Any(x => String.Equals(x.Login, registratedUser.Login))) { throw new UserLoginException(); } // Check Email unique // SELECT Email from Persons WHERE Curator_Id == null if (clientRepository.Context.Persons.Include("Curator").Any(x => String.Equals(x.Email, registratedUser.Email) && x.Curator == null)) { throw new UserEmailException(); } // Add row in Persons Person person = new Person() { FirstName = registratedUser.FirstName, LastName = registratedUser.LastName, MiddleName = registratedUser.MiddleName, BirthDate = registratedUser.BirthDate.Value, Gender = registratedUser.Gender.Value, Email = registratedUser.Email }; clientRepository.Context.Persons.Add(person); clientRepository.Context.SaveChanges(); // Add row in Clients Client client = new Client() { Login = registratedUser.Login, Password = registratedUser.Password, PersonId = person.Id, IsCurator = false, IsAdmin = false }; if(!clientRepository.Create(client).Status) { throw new CreateException(); } // Add row in Authorizations Authorization authorization = new Authorization(); authorization.Token = Guid.NewGuid(); authorization.ClientId = client.Id; authorization.ExpirationDate = DateTime.Now.AddMinutes(Constraints.KExpirationMinutes); clientRepository.Context.Authorizations.Add(authorization); clientRepository.Context.SaveChanges(); registratedUser.Id = client.Id; registratedUser.IsCurator = client.IsCurator; registratedUser.IsAdmin = client.IsAdmin; registratedUser.PersonId = person.Id; registratedUser.Token = authorization.Token; registratedUser.ExpirationDate = authorization.ExpirationDate; return registratedUser; } }
public static RegistrationContent GetUser(Guid token) { using (var clientRepository = new BaseRepository<Client>()) { Authorization authorization = clientRepository.Context.Authorizations.FirstOrDefault(x => x.Token == token); if (authorization == null) { throw new UnauthorizedException(); } Client foundedClient = clientRepository.GetAllItems.FirstOrDefault(x => x.Id == authorization.ClientId); if (foundedClient == null) { throw new UserDoesNotExistException(); } Person personRelated = clientRepository.Context.Persons.FirstOrDefault(x => x.Id == foundedClient.PersonId); if (personRelated == null) { throw new PersonDoesNotExistException(); } // Form output registration content RegistrationContent content = new RegistrationContent(); content.Id = foundedClient.Id; content.Login = foundedClient.Login; content.Password = foundedClient.Password; content.IsCurator = foundedClient.IsCurator; content.IsAdmin = foundedClient.IsAdmin; content.PersonId = personRelated.Id; content.FirstName = personRelated.FirstName; content.LastName = personRelated.LastName; content.MiddleName = personRelated.MiddleName; content.Gender = personRelated.Gender; content.BirthDate = personRelated.BirthDate; content.Email = personRelated.Email; content.Token = authorization.Token; content.ExpirationDate = authorization.ExpirationDate; return content; } }