Exemplo n.º 1
0
 /**
  * Loads the “Invite” model with the given primary key
  * 
  * @param primary key to load
  * @param dummy value to conform with Sprig
  * @return Model_Invite
  */
 public static function factory($token, array $dummy = array())
 {
     if (Model_Invite::valid($token)) {
         $dummy['token'] = $token;
     }
     return parent::factory('invite', $dummy);
 }
Exemplo n.º 2
0
 /**
  * Registration procedure (from a given invitation)
  */
 public function action_register()
 {
     $token = arr::get($_GET, 'token', NULL);
     if (!Model_Invite::valid($token)) {
         $this->message_add('Din inbjudan är ogiltig.', 'error');
         // TODO: better message
         $this->request->redirect_back();
     } else {
         $invite = Model_Invite::factory($token)->load();
     }
     // Registrera användare
     if (!empty($_POST)) {
         $_POST['email'] = $invite->email;
         $_POST['logins'] = 0;
         $user = Sprig::factory('user', $_POST);
         /**
          * The following is executed as a transaction, and rolls
          * back if something goes wrong
          */
         try {
             // Start transaction
             DB::query(NULL, 'BEGIN')->execute();
             $user->create();
             $user->add('roles', array(Sprig::factory('role', array('name' => 'login'))->load(), Sprig::factory('role', array('name' => 'ängel'))->load()))->update();
             // Make invite invalid
             $invite->values(array('invitee' => $user->id))->update();
             // Commit transaction
             DB::query(NULL, 'COMMIT')->execute();
             // Log in!
             $this->auth->login($_POST['username'], $_POST['password']);
             // Welcome message and redirect to control panel
             $this->message_add(sprintf('Välkommen, %s! Du är nu registrerad och inloggad.
                     Jag tog mig också friheten att dirigera dig till forumet.', html::chars($user->username)));
             // TODO: Make message
             $this->request->redirect('forum', 303);
         } catch (Exception $e) {
             // Rollback transaction (innodb ftw)
             DB::query(NULL, 'ROLLBACK')->execute();
             // Non-validation errors are re-thrown and logged
             if (!$e instanceof Validate_Exception) {
                 throw $e;
             }
             // Show validation errors
             foreach ($e->array->errors('user/register') as $error) {
                 $this->message_add($error, 'error');
             }
         }
         $this->request->reload();
     }
     $this->template->content = View::factory('user/register')->set('invite', $invite);
 }