Psecio\Gatekeeper\Gatekeeper::register PHP Method

register() public static method

Register a new user
public static register ( array $userData ) : boolean
$userData array User data
return boolean Success/fail of user create
    public static function register(array $userData)
    {
        $user = new UserModel(self::$datasource, $userData);
        if (self::$datasource->save($user) === false) {
            return false;
        }
        // Add groups if they're given
        if (isset($userData['groups'])) {
            foreach ($userData['groups'] as $group) {
                $group = is_int($group) ? self::findGroupById($group) : self::findGroupByName($group);
                $user->addGroup($group);
            }
        }
        // Add permissions if they're given
        if (isset($userData['permissions'])) {
            foreach ($userData['permissions'] as $perm) {
                $perm = is_int($perm) ? self::findPermissionById($perm) : self::findPermissionByName($perm);
                $user->addPermission($perm);
            }
        }
        return $user;
    }

Usage Example

Example #1
0
 public function processSignupAction()
 {
     try {
         v::email()->check($_POST['email']);
         v::length(6)->check($_POST['password']);
     } catch (ValidationException $e) {
         $this->flasher->error('Please make sure your password is longer than 6 characters, and that your username is a valid email address!');
     }
     if ($_POST['password'] !== $_POST['password_confirm']) {
         $this->flasher->error('Passwords need to be identical');
     }
     if ($this->flasher->hasMessages('error')) {
         $this->redirect('/auth');
     }
     $this->initGroups();
     // Create an account if none exists
     $user = Gatekeeper::register(['first_name' => '-', 'last_name' => '-', 'username' => $_POST['email'], 'email' => $_POST['email'], 'password' => $_POST['password'], 'groups' => Gatekeeper::countUser() ? ['users'] : ['admin', 'users']]);
     if ($user) {
         $this->flasher->success('Account successfully registered! Please log in!');
     } else {
         $this->flasher->error('Error #GK01: Account creation failed!' . Gatekeeper::getDatasource()->getLastError());
     }
     $this->redirect('/auth');
 }
All Usage Examples Of Psecio\Gatekeeper\Gatekeeper::register