Ejemplo n.º 1
0
    /**
     * Register is basically the same as create. It just lets us use a separeate "register" template in addition to a "create" one.
     * However, it's a front-end action and as such it can never allow a user role to be set to anything more than a regular user.
     * 
    */
    public function register() {
        // Get the fields so the view template can iterate through them and build the form
        $fields = User::schema();
        // Don't need to have these fields in the form
        unset($fields[User::key()]);
        
        $rules = array(
            'email' => array(
                array('notEmpty', 'message' => 'E-mail cannot be empty.'),
                array('email', 'message' => 'E-mail is not valid.'),
                array('uniqueEmail', 'message' => 'Sorry, this e-mail address is already registered.'),
            ),
            'password' => array(
                array('notEmpty', 'message' => 'Password cannot be empty.'),
                array('notEmptyHash', 'message' => 'Password cannot be empty.'),
                array('moreThanFive', 'message' => 'Password must be at least 6 characters long.')
            )
            // TODO: password confirm
        );
        
        // Save
        if ($this->request->data) {
            $user = User::create();
	    $this->request->data['role'] = 'registered_user'; // set basic user, always hard coded and set
	    
	    // IF this is the first user ever created, then they will be an administrator
	    // TODO: make a wizard that will set this so there's no chance of some user registering and becoming an admin
	    $users = User::find('count');
	    if(empty($users)) {
		$this->request->data['role'] = 'administrator';
		$this->request->data['active'] = true;
	    }
	    
	    // Make sure there's a user type (default is "user" a normal user that might have access to the backend based on their role)
	    if((!isset($this->request->data['user_type'])) || (empty($this->request->data['user_type']))) {
		//$this->request->data['user_type'] = 'user';
		$this->request->data['user_type'] = null;
	    }
	    
            if($user->save($this->request->data, array('validate' => $rules))) {
                //$this->redirect(array('controller' => 'users', 'action' => 'index'));
                $this->redirect('/');
            }
        }
        
        if(empty($user)) {
            // Create an empty user object
            $user = User::create();
        }
        
        $this->set(compact('user', 'fields'));
    }