Autodoc
  • Namespace
  • Class
  • Tree

Namespaces

  • BlueTihi
    • Context
  • Brickrouge
    • Element
      • Nodes
    • Renderer
    • Widget
  • ICanBoogie
    • ActiveRecord
    • AutoConfig
    • CLDR
    • Composer
    • Core
    • Event
    • Exception
    • HTTP
      • Dispatcher
      • Request
    • I18n
      • Translator
    • Mailer
    • Modules
      • Taxonomy
        • Support
      • Thumbnailer
        • Versions
    • Object
    • Operation
      • Dispatcher
    • Prototype
    • Routes
    • Routing
      • Dispatcher
    • Session
  • Icybee
    • ActiveRecord
      • Model
    • ConfigOperation
    • Document
    • EditBlock
    • Element
      • ActionbarContextual
      • ActionbarSearch
      • ActionbarToolbar
    • FormBlock
    • Installer
    • ManageBlock
    • Modules
      • Articles
      • Cache
        • Collection
        • ManageBlock
      • Comments
        • ManageBlock
      • Contents
        • ManageBlock
      • Dashboard
      • Editor
        • Collection
      • Files
        • File
        • ManageBlock
      • Forms
        • Form
        • ManageBlock
      • I18n
      • Images
        • ManageBlock
      • Members
      • Modules
        • ManageBlock
      • Nodes
        • ManageBlock
        • Module
      • Pages
        • BreadcrumbElement
        • LanguagesElement
        • ManageBlock
        • NavigationBranchElement
        • NavigationElement
        • Page
        • PageController
      • Registry
      • Search
      • Seo
      • Sites
        • ManageBlock
      • Taxonomy
        • Terms
          • ManageBlock
        • Vocabulary
          • ManageBlock
      • Users
        • ManageBlock
        • NonceLogin
        • Roles
      • Views
        • ActiveRecordProvider
        • Collection
        • View
    • Operation
      • ActiveRecord
      • Constructor
      • Module
      • Widget
    • Rendering
  • None
  • Patron
  • PHP

Classes

  • Hooks
  • Module
  • NonceLoginOperation
  • NonceLoginRequestOperation
  • NonceRequestForm
  • Ticket
  • TicketModel
  1 <?php
  2 
  3 /*
  4  * This file is part of the Icybee package.
  5  *
  6  * (c) Olivier Laviale <olivier.laviale@gmail.com>
  7  *
  8  * For the full copyright and license information, please view the LICENSE
  9  * file that was distributed with this source code.
 10  */
 11 
 12 namespace Icybee\Modules\Users\NonceLogin;
 13 
 14 use ICanBoogie\DateTime;
 15 use ICanBoogie\PermissionRequired;
 16 use ICanBoogie\HTTP\Request;
 17 
 18 /**
 19  * Provides a nonce login.
 20  *
 21  * @property-read \Icybee\Modules\Users\User $user The user for which a ticket should be created.
 22  * Alias for {@link $record}.
 23  * @property-read Ticket $ticket The ticket created by the operation.
 24  */
 25 class NonceLoginRequestOperation extends \ICanBoogie\Operation
 26 {
 27     /**
 28      * @todo-20131009: remove this when Operation is cleverer.
 29      */
 30     public function __construct($request=null)
 31     {
 32         global $core;
 33 
 34         parent::__construct($request);
 35 
 36         $this->module = $core->modules['users.noncelogin'];
 37     }
 38 
 39     /**
 40      * Returns the record assocaiated with the email address specified by the `email` param.
 41      *
 42      * @return User|null
 43      */
 44     protected function lazy_get_record()
 45     {
 46         global $core;
 47 
 48         $email = $this->request['email'];
 49 
 50         if (!$email)
 51         {
 52             return;
 53         }
 54 
 55         /* @var $record \Icybee\Modules\Users\User */
 56 
 57         $record = $core->models['users']->filter_by_email($email)->one;
 58 
 59         if ($record && $record->constructor != 'users')
 60         {
 61             $record = $core->models[$record->constructor][$record->uid];
 62         }
 63 
 64         return $record;
 65     }
 66 
 67     /**
 68      * Returns the user for which a ticket should be created.
 69      *
 70      * @return \Icybee\Modules\Users\User
 71      */
 72     protected function get_user()
 73     {
 74         return $this->record;
 75     }
 76 
 77     private $ticket;
 78 
 79     /**
 80      * Returns the ticket created by the operation.
 81      *
 82      * @return Ticket
 83      */
 84     protected function get_ticket()
 85     {
 86         return $this->ticket;
 87     }
 88 
 89     protected function validate(\ICanboogie\Errors $errors)
 90     {
 91         global $core;
 92 
 93         $email = $this->request['email'];
 94 
 95         if (!$email)
 96         {
 97             $errors['email'] = $errors->format('The field %field is required!', array('%field' => 'Votre adresse E-Mail'));
 98 
 99             return false;
100         }
101 
102         if (!filter_var($email, FILTER_VALIDATE_EMAIL))
103         {
104             $errors['email'] = $errors->format("Invalid email address: %email.", array('%email' => $email));
105 
106             return false;
107         }
108 
109         $user = $this->record;
110 
111         if (!$user)
112         {
113             $errors['email'] = $errors->format("Unknown email address.");
114 
115             return false;
116         }
117 
118         if ($user->language)
119         {
120             $core->locale = $user->language;
121         }
122 
123         $expire_at = null;
124         $ticket = $this->module->model->filter_by_uid($user->uid)->one;
125 
126         if ($ticket)
127         {
128             $expire_at = $ticket->expire_at;
129         }
130 
131         if ($expire_at && (time() + Module::FRESH_PERIOD - $expire_at->timestamp < Module::COOLOFF_DELAY))
132         {
133             throw new PermissionRequired
134             (
135                 $errors->format("nonce_login_request.operation.already_sent", array
136                 (
137                     ':time' => DateTime::from('@' . ($expire_at->timestamp - Module::FRESH_PERIOD + Module::COOLOFF_DELAY), 'utc')->local->format('H:i')
138                 )),
139 
140                 403
141             );
142         }
143 
144         return true;
145     }
146 
147     /**
148      * Creates a nonce login ticket.
149      *
150      * If a previous ticket for the user exists it will be deleted.
151      */
152     protected function process()
153     {
154         global $core;
155 
156         $user = $this->record;
157         $model = $this->module->model;
158 
159         # delete previous ticket (if any)
160 
161         $model->filter_by_uid($user->uid)->delete();
162 
163         # create new ticket
164 
165         $ticket = Ticket::from(array(
166 
167             'uid' => $user->uid,
168             'token' => $model->generate_token(),
169             'expire_at' => '+' . Module::FRESH_PERIOD . ' seconds',
170             'ip' => $this->request->ip
171 
172         ));
173 
174         $ticket->save();
175 
176         $this->ticket = $ticket;
177         $this->response->message = $errors->format('success', array('%email' => $user->email), array('scope' => \ICanBoogie\normalize($user->constructor, '_') . '.nonce_login_request.operation'));
178 
179         return true;
180     }
181 }
Autodoc API documentation generated by ApiGen 2.8.0