Ejemplo n.º 1
0
        public AccountDTO CreateAccount(RegistrationForm registrationForm)
        {
            var role           = this.accountUnitOfWork.RoleRepository.Get(r => r.Name == registrationForm.Role).FirstOrDefault();
            var accountAddress = this.accountUnitOfWork.AddressRepository.GetAddressesByPlace(
                registrationForm.PersonalData.Address.Country,
                registrationForm.PersonalData.Address.City,
                registrationForm.PersonalData.Address.Street,
                registrationForm.PersonalData.Address.House,
                registrationForm.PersonalData.Address.Apartments).FirstOrDefault()
                                 ?? this.accountUnitOfWork.AddressRepository.Create(
                ObjectMapper <AddressDTO, AddressEntity> .Map(registrationForm.PersonalData.Address));

            var personalData = ObjectMapper <PersonalDataDTO, PersonalDataEntity> .Map(registrationForm.PersonalData);

            personalData.AddressEntity = accountAddress;

            AccountEntity account;

            try
            {
                var credentials = new CredentialsEntity(registrationForm.Login, PasswordHasher.Hash(registrationForm.Password), role.Id);
                account = this.accountUnitOfWork.AccountRepository.Create(new AccountEntity(personalData, credentials));
                this.accountUnitOfWork.SaveChanges();
            }
            catch (NullReferenceException)
            {
                return(null);
            }

            return(ObjectMapper <AccountEntity, AccountDTO> .Map(account));
        }
Ejemplo n.º 2
0
        public bool InsertUser(CredentialsEntity credentials, UserPropertiesEntity userProperties,
                               UserPermissionsEntity userPermissions)
        {
            var sqlConnection = DatabaseConnection.Instance.GetConnection();
            var trasaction    = sqlConnection.BeginTransaction("Insert user transaction");

            try
            {
                var insertUserCredentialsQuery = string.Format("INSERT INTO {0} VALUES ('{1}','{2}')", DbConstants.UsersTable,
                                                               credentials.UserName, credentials.Password);

                var sqlCommand = sqlConnection.CreateCommand();
                sqlCommand.Transaction = trasaction;

                sqlCommand.CommandText = insertUserCredentialsQuery;

                sqlCommand.ExecuteNonQuery();

                var getUserIdQuery = string.Format("Select Id From {0} where {1}='{2}' and {3}='{4}'",
                                                   DbConstants.UsersTable, DbConstants.UserTable_UserName, credentials.UserName, DbConstants.UserTable_PasswordColoumn,
                                                   credentials.Password);

                sqlCommand.CommandText = getUserIdQuery;
                var sqlDataReader = sqlCommand.ExecuteReader();
                sqlDataReader.Read();
                var userId = (int)sqlDataReader[0];
                sqlDataReader.Close();

                var insertIntoUserPropertiesQuery = string.Format("Insert Into {0} Values({1},'{2}','{3}','{4}', {5})",
                                                                  DbConstants.UserPropertiesTable, userId, userProperties.Title, userProperties.FirstName,
                                                                  userProperties.LastName, 0);

                sqlCommand.CommandText = insertIntoUserPropertiesQuery;
                sqlCommand.ExecuteNonQuery();

                var insertIntoUserPermissionsQuery = string.Format("Insert Into {0} Values({1}, {2}, {3}, {4}, {5}, {6}, {7})",
                                                                   "UserPermissions",
                                                                   userId,
                                                                   userPermissions.CanCreateProject.GetBitValue(),
                                                                   userPermissions.CanEditProject.GetBitValue(),
                                                                   userPermissions.CanCreateTask.GetBitValue(),
                                                                   userPermissions.CanEditTask.GetBitValue(),
                                                                   userPermissions.CanAssignTask.GetBitValue(),
                                                                   userPermissions.CanCloseTask.GetBitValue());

                sqlCommand.CommandText = insertIntoUserPermissionsQuery;
                sqlCommand.ExecuteNonQuery();

                trasaction.Commit();
                return(true);
            }
            catch (Exception)
            {
                trasaction.Rollback();
                return(false);
            }
        }
Ejemplo n.º 3
0
        private int GetUserId(CredentialsEntity credentials)
        {
            var isUserPresentCheckQuery = string.Format("Select Id From {0} where {1}='{2}' and {3}='{4}'",
                                                        DbConstants.UsersTable, DbConstants.UserTable_UserName, credentials.UserName, DbConstants.UserTable_PasswordColoumn,
                                                        credentials.Password);

            var sqlCommand    = GetSqlCommand(isUserPresentCheckQuery);
            var sqlDataReader = sqlCommand.ExecuteReader();

            if (sqlDataReader.Read())
            {
                return((int)sqlDataReader[0]);
            }

            return(-1);
        }
Ejemplo n.º 4
0
        public UserEntity GetUserEntity(CredentialsEntity credentials)
        {
            var userId = GetUserId(credentials);

            if (userId < 1)
            {
                return(null);
            }

            var userPropertiesEntity  = GetUserPropertiesEntity(userId);
            var userPermissionsEntity = GetUserPermissionsEntity(userId);

            if (userPropertiesEntity == null || userPermissionsEntity == null)
            {
                return(null);
            }

            return(new UserEntity(userId, userPropertiesEntity, userPermissionsEntity));
        }
Ejemplo n.º 5
0
 public void Update(CredentialsEntity item)
 {
     _credentials.RemoveAll(dto => dto.Id == item.Id);
     _credentials.Add(item);
 }
Ejemplo n.º 6
0
 public void Create(CredentialsEntity item)
 {
     _credentials.Add(item);
 }