/** * Creates user * * @param array $array Array of parameters * @throws ModelException */ public function create(array $array) { $user = new User(); $this->convertDates($array, ['birth_date']); $user->populate($array); $existingUser = $this->dao->getUserByLogin($user->login); if (!is_null($existingUser)) { throw new ModelException('This login already exists'); } $passwordManager = new PasswordManager(); $salt = $passwordManager->getRandomSalt(); $password = $passwordManager->getHash($array['password'], $salt); $user->password = $password; $user->salt = $salt; $this->dao->save($user); }
/** * Creates file * * @param array $uploadedFile Array representation of uploaded file * @param int $userId User ID * @throws ModelException */ public function create(array $uploadedFile, $userId) { if (!$this->canAddFiles($userId)) { throw new ModelException('You can\'t add more files'); } switch ($uploadedFile['error']) { case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: throw new ModelException('The uploaded file exceeds the maximum file size'); case UPLOAD_ERR_PARTIAL: throw new ModelException('The uploaded file was only partially uploaded'); case UPLOAD_ERR_NO_FILE: throw new ModelException('No file was uploaded'); case UPLOAD_ERR_NO_TMP_DIR: throw new ModelException('Missing a temporary folder'); case UPLOAD_ERR_CANT_WRITE: throw new ModelException('Failed to write file to disk'); } if (is_uploaded_file($uploadedFile['tmp_name'])) { $file = new File(); // Generate file hash $passwordManager = new PasswordManager(); $hash = $passwordManager->getRandomPassword(50); $file->hash = $hash; if (!move_uploaded_file($uploadedFile['tmp_name'], UPLOADED_FILES . $file->hash)) { throw new ModelException('Uploaded file can\'t be saved'); } // Set user ID $file->userId = $userId; // Set file size $file->size = $uploadedFile['size']; // Set original name $file->origName = $uploadedFile['name']; $this->dao->save($file); } }
/** * Makes user authorization * * @param string $login Login * @param string $password Password * @param bool|false $remember Remember flag state * @throws IncorrectPasswordException * @throws LoginNotFoundException */ public function login($login, $password, $remember = false) { $login = trim($login); $user = DaoFactory::getUserDao()->getUserByLogin($login); if (!is_null($user)) { $psw = new PasswordManager(); if ($user->getPassword() !== $psw->getHash($password, $user->getSalt())) { throw new IncorrectPasswordException('Incorrect password'); } else { $id = $user->getId(); $this->makeSession($id); if ($remember) { $this->makeCookie('field1', $id); $this->makeCookie('field2', $this->getSecretPhrase($id, $login, $password)); } } } else { throw new LoginNotFoundException('User not found'); } }