Пример #1
0
 /**
  * Create new user
  *
  * @param   string
  * @param   string
  * @param   string  must contain valid email address
  * @param   int     group id
  * @param   Array
  * @return  bool
  */
 public function create_user($username, $password, $email, $group = 1, array $profile_fields = array())
 {
     // prep the password
     $password = trim($password);
     // and validate the email address
     $email = filter_var(trim($email), FILTER_VALIDATE_EMAIL);
     // bail out if we're missing username, password or email address
     if (empty($username) or empty($password) or empty($email)) {
         throw new \SimpleUserUpdateException('Username, password or email address is not given, or email address is invalid', 1);
     }
     // check if we already have an account with this email address or username
     $duplicate = \Model\Auth_User::query()->select(\Config::get('ormauth.table_columns', array()))->where('username', '=', $username)->or_where('email', '=', $email)->get_one();
     // did we find one?
     if ($duplicate) {
         // bail out with an exception
         if (strtolower($email) == strtolower($duplicate->email)) {
             throw new \SimpleUserUpdateException('Email address already exists', 2);
         } else {
             throw new \SimpleUserUpdateException('Username already exists', 3);
         }
     }
     // do we have a logged-in user?
     if ($currentuser = \Auth::get_user_id()) {
         $currentuser = $currentuser[1];
     } else {
         $currentuser = 0;
     }
     // create the new user record
     $user = \Model\Auth_User::forge(array('username' => (string) $username, 'password' => $this->hash_password((string) $password), 'email' => $email, 'group_id' => (int) $group, 'last_login' => 0, 'previous_login' => 0, 'login_hash' => '', 'user_id' => $currentuser, 'created_at' => \Date::forge()->get_timestamp(), 'updated_at' => 0));
     // we don't use profile fields, store the data in the metadata table instead
     foreach ($profile_fields as $field => $value) {
         $user->metadata[] = \Model\Auth_Metadata::forge(array('key' => $field, 'value' => $value));
     }
     // save the new user record
     try {
         $result = $user->save();
     } catch (\Exception $e) {
         $result = false;
     }
     // and the id of the created user, or false if creation failed
     return $result ? $user->id : false;
 }