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

  • ActivateOperation
  • AvailableSitesBlock
  • ConfigBlock
  • ConfigOperation
  • DeactivateOperation
  • DeleteBlock
  • DeleteOperation
  • EditBlock
  • Hooks
  • IsUniqueOperation
  • LoginComboElement
  • LoginForm
  • LoginOperation
  • LogoutOperation
  • ManageBlock
  • Model
  • Module
  • OwnershipResolver
  • PermissionResolver
  • ProfileController
  • QueryOperationOperation
  • SaveOperation
  • UnlockLoginOperation
  • Update20131021
  • User
  • ViewProvider

Interfaces

  • OwnershipResolverInterface
  • PermissionResolverInterface

Traits

  • LoggedAtProperty

Exceptions

  • WebsiteAdminNotAccessible
  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;
 13 
 14 /**
 15  * Create or update a user profile.
 16  */
 17 class SaveOperation extends \Icybee\Operation\Constructor\Save
 18 {
 19     protected function lazy_get_properties()
 20     {
 21         global $core;
 22 
 23         $properties = parent::lazy_get_properties();
 24         $request = $this->request;
 25 
 26         if ($request[User::PASSWORD])
 27         {
 28             $properties[User::PASSWORD] = $request[User::PASSWORD];
 29         }
 30 
 31         if ($core->user->has_permission(Module::PERMISSION_ADMINISTER, $this->module))
 32         {
 33             #
 34             # roles - because roles are not in the properties we need to prepare them for the
 35             # model using the params.
 36             #
 37 
 38             $roles = [];
 39 
 40             if ($request[User::ROLES])
 41             {
 42                 foreach ($request[User::ROLES] as $rid => $value)
 43                 {
 44                     $value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
 45 
 46                     if (!$value)
 47                     {
 48                         continue;
 49                     }
 50 
 51                     $roles[] = (int) $rid;
 52                 }
 53             }
 54 
 55             $properties[User::ROLES] = $roles;
 56 
 57             #
 58             # restricted sites - because restricted sites are not in the properties we need to
 59             # prepare them for the model using the params.
 60             #
 61 
 62             $sites = [];
 63 
 64             if ($request[User::RESTRICTED_SITES])
 65             {
 66                 foreach ($request[User::RESTRICTED_SITES] as $siteid => $value)
 67                 {
 68                     $value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
 69 
 70                     if (!$value)
 71                     {
 72                         continue;
 73                     }
 74 
 75                     $sites[] = (int) $siteid;
 76                 }
 77             }
 78 
 79             $properties[User::RESTRICTED_SITES] = $sites;
 80         }
 81         else
 82         {
 83             unset($properties[User::IS_ACTIVATED]);
 84         }
 85 
 86         return $properties;
 87     }
 88 
 89     /**
 90      * Permission is granted if the user is modifing its own profile, and has permission to.
 91      *
 92      * @see ICanBoogie.Operation::control_permission()
 93      */
 94     protected function control_permission($permission=Module::PERMISSION_CREATE)
 95     {
 96         global $core;
 97 
 98         $user = $core->user;
 99 
100         if ($user->uid == $this->key && $user->has_permission('modify own profile'))
101         {
102             return true;
103         }
104 
105         return parent::control_permission($permission);
106     }
107 
108     protected function control_ownership()
109     {
110         global $core;
111 
112         $user = $core->user;
113 
114         if ($user->uid == $this->key && $user->has_permission('modify own profile'))
115         {
116             // TODO-20110105: it this ok to set the user as a record here ?
117 
118             $this->record = $user;
119 
120             return true;
121         }
122 
123         return parent::control_ownership();
124     }
125 
126     /**
127      * The 'User' role (rid 2) is mandatory for every user.
128      *
129      * @see ICanBoogie.Operation::control_form()
130      */
131     protected function control_form()
132     {
133         $this->request->params[User::ROLES][2] = 'on';
134 
135         return parent::control_form($this);
136     }
137 
138     protected function validate(\ICanboogie\Errors $errors)
139     {
140         global $core;
141 
142         $properties = $this->properties;
143 
144         if (!empty($properties[User::PASSWORD]))
145         {
146             if (!$this->request[User::PASSWORD . '-verify'])
147             {
148                 $errors[User::PASSWORD . '-verify'] = $errors->format('Password verify is empty.');
149             }
150 
151             if ($properties[User::PASSWORD] != $this->request[User::PASSWORD . '-verify'])
152             {
153                 $errors[User::PASSWORD . '-verify'] = $errors->format("Password and password verify don't match.");
154             }
155         }
156 
157         $uid = $this->key ? $this->key : 0;
158         $model = $core->models['users'];
159 
160         #
161         # unique username
162         #
163 
164         if (isset($properties[User::USERNAME]))
165         {
166             $username = $properties[User::USERNAME];
167             $used = $model->select('uid')->where('username = ? AND uid != ?', $username, $uid)->rc;
168 
169             if ($used)
170             {
171                 $errors[User::USERNAME] = $errors->format("L'identifiant %username est déjà utilisé.", [
172 
173                     '%username' => $username
174 
175                 ]);
176             }
177         }
178 
179         #
180         # check if email is unique
181         #
182 
183         if (isset($properties[User::EMAIL]))
184         {
185             $email = $properties[User::EMAIL];
186             $used = $model->select('uid')->where('email = ? AND uid != ?', $email, $uid)->rc;
187 
188             if ($used)
189             {
190                 $errors[User::EMAIL] = $errors->format("L'adresse email %email est déjà utilisée.", [
191 
192                     '%email' => $email
193 
194                 ]);
195             }
196         }
197 
198         return count($errors) == 0 && parent::validate($errors);
199     }
200 
201     protected function process()
202     {
203         global $core;
204 
205         $previous_uid = $core->user_id;
206 
207         $rc = parent::process();
208 
209         $uid = $rc['key'];
210 
211         if (!$previous_uid)
212         {
213             $this->response->message = $errors->format("Your profile has been created.");
214         }
215         else if ($core->user_id == $uid)
216         {
217             $this->response->message = $errors->format($rc['mode'] == 'update' ? "Your profile has been updated." : "Your profile has been created.");
218         }
219         else
220         {
221             $record = $this->module->model[$uid];
222 
223             $this->response->message = $errors->format($rc['mode'] == 'update' ? "%name's profile has been updated." : "%name's profile has been created.", [ 'name' => $record->name ]);
224         }
225 
226         return $rc;
227     }
228 }
Autodoc API documentation generated by ApiGen 2.8.0