예제 #1
0
 public function addUser()
 {
     try {
         $min_data = ['nickname', 'email', 'password'];
         $form = $this->getApp()->request()->post();
         foreach ($min_data as $required_field) {
             if (!isset($form[$required_field])) {
                 throw new \Exception("Missing required field " . $required_field . ". Required fields are " . implode(",", $min_data));
             }
         }
         $email = $form["email"];
         $password = $form["password"];
         $nickname = $form["nickname"];
         if (!Utilities::isValidEmail($email)) {
             throw new \Exception(Language::t('USERS_NO_USER_WITH_EMAIL', "Email " . $email . " is not a valid email address"));
         }
         //-- Before creating a new user, we need to check we dont have a user with this same user name
         $existing_user = UserAuth::getByUserName($email);
         if (!is_null($existing_user)) {
             throw new \Exception("Email " . $email . " is already in use");
         }
         //-- Ok, lets create the new User
         $user_auth = new UserAuth();
         $user_auth->setUserName($email);
         //-- Lets encode the password using a salt string
         $salt_string = strval(time());
         $password = $password . $salt_string;
         $user_auth->setPassword(sha1($password));
         $user_auth->setSalt(base64_encode($salt_string));
         $user_auth->persist();
         //-- Now we have the user_id, lets create the user now
         $user = new User();
         $user->setUserId($user_auth->getUserId());
         $user->setEmail($email);
         $user->setNickname($nickname);
         $user->persist();
         //-- Remove Password from Response
         unset($form['password']);
         $this->getApp()->render(200, ['data' => $form]);
     } catch (\Exception $e) {
         $this->getApp()->render(500, ['error' => $e->getMessage()]);
     }
 }
예제 #2
0
파일: User.php 프로젝트: bathan/fumbol_api
 private static function createFromDb($resource)
 {
     if (!is_null($resource)) {
         $user = new User();
         $user->setRowId($resource["row_id"]);
         $user->setUserId($resource["user_id"]);
         $user->setFirstName($resource["first_name"]);
         $user->setLastName($resource["last_name"]);
         $user->setNickname($resource["nickname"]);
         $user->setEmail($resource["email"]);
         $user->setCreatedDate($resource["created_date"]);
         return $user;
     }
     return null;
 }