示例#1
0
        public PersonContent AuthorizePerson(String accessKey)
        {
            using(var personRepository = new BaseRepository<Person>())
            {
                SurveysPersonsGroup surveysPersonsGroup =
                    personRepository.Context.SurveysPersonsGroups.FirstOrDefault(
                        x => String.Equals(x.AccessKey, accessKey));
                if(surveysPersonsGroup == null)
                {
                    throw new PersonDoesNotExistException();
                }
                Person person = surveysPersonsGroup.Person;

                Authorization authorization =
                    personRepository.Context.Authorizations.FirstOrDefault(
                        x => x.SurveysPersonId == surveysPersonsGroup.Id);
                if(authorization == null)
                {
                    authorization = new Authorization()
                                        {
                                            SurveysPersonId = surveysPersonsGroup.Id,
                                            Token = Guid.NewGuid(),
                                            ExpirationDate = DateTime.Now.AddMinutes(Constraints.KExpirationMinutes),
                                        };
                    personRepository.Context.Authorizations.Add(authorization);
                }
                else
                {
                    // Update row in Authorizations
                    if (authorization.ExpirationDate < DateTime.Now)
                    {
                        authorization.Token = Guid.NewGuid();
                    }
                    authorization.ExpirationDate = DateTime.Now.AddMinutes(Constraints.KExpirationMinutes);
                    personRepository.Context.Entry(authorization).State = EntityState.Modified;
                }
                personRepository.Context.SaveChanges();

                //Fill person content
                PersonContent content = new PersonContent();
                content.Id = surveysPersonsGroup.Id;
                content.PersonId = person.Id;
                content.AccessKey = surveysPersonsGroup.AccessKey;
                content.SurveyResultId = surveysPersonsGroup.SurveysResult.Id;
                content.Gender = person.Gender;
                content.Email = person.Email;
                content.BirthDate = person.BirthDate.Value;
                content.FirstName = person.FirstName;
                content.LastName = person.LastName;
                content.MiddleName = person.MiddleName;
                content.Note = person.Note;
                if (person.Curator != null)
                {
                    content.CuratorId = person.Curator.Id;
                }
                else
                {
                    content.CuratorId = 0;
                }
                content.ExpirationDate = authorization.ExpirationDate;
                content.Token = authorization.Token;
                //content.SurveysPersonId = surveysPersonsGroup.Id;

                return content;
            }
        }
示例#2
0
        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;

            }
        }
示例#3
0
        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;

            }
        }