Beispiel #1
0
 /**
  * Archive a record
  * @parma string $model reference of the model instance
  * @param UUID $id of the record to archive
  * @param boolean $restore, archive or restore?
  */
 function archive(&$Model, $id, $restore = false)
 {
     $success = false;
     if (Common::isUuid($id)) {
         $Model->data = $Model->read(null, $id);
         // check if exist
         if (!isset($Model->data[$Model->alias][$this->__settings[$Model->alias]['archive_field']])) {
             throw new Exception(__('The resource doesn\'t exist. It may have been deleted.', true));
         } elseif ($Model->data[$Model->alias][$this->__settings[$Model->alias]['archive_field']] != $restore) {
             if ($restore) {
                 throw new Exception(__('The resource is already active / restored.', true));
             } else {
                 throw new Exception(__('The resource is already archived.', true));
             }
         } else {
             $Model->data = array();
             $Model->id = $id;
             $Model->data[$Model->alias][$this->__settings[$Model->alias]['archive_field']] = !$restore;
             $success = $Model->save($Model->data);
         }
     } else {
         throw new Exception(__('The given ID is invalid.', true));
     }
     return $success;
 }
Beispiel #2
0
 /**
  * Before Save callback
  * @param $Model
  */
 function beforeSave(&$Model)
 {
     $action = Common::isUuid($Model->id) ? 'update' : 'create';
     $details = print_r($Model->data, true);
     $this->__deferred = true;
     $this->__log($Model->alias, $action, $details, $Model->id);
 }
 /**
  * Edit a given project
  * @param $id project uuid
  * @return void
  * @access public
  */
 function edit($id = null)
 {
     $this->uuid['action'] = 'df8e4a16-8141-11e0-a245-000ae4cc0097';
     // some data where submited
     if (!empty($this->data)) {
         $this->Project->id = $id;
         if ($this->Project->save($this->data)) {
             $this->Message->success(sprintf(__('The project was sucessfully saved (%s)', true), $this->Project->id), array('action' => 'index'));
         } else {
             $this->Message->error('ERROR_PROJECT_EDIT_SAVE', sprintf(__('The project could not be saved, please correct the errors bellow (%)', true), $this->Project->id));
         }
     } else {
         $error = true;
         if (!empty($id) && Common::isUuid($id)) {
             $project = $this->Project->read(null, $id);
             if (!empty($project)) {
                 $error = false;
                 $this->data = $project;
             }
         }
         if ($error) {
             $this->Message->error('ERROR_INVALID_PROJECT_ID', __('Sorry, this project is invalid or have been deleted', true), array('action' => 'index'));
         }
     }
 }
Beispiel #4
0
 /**
  * undocumented function
  *
  * @param unknown $key 
  * @param unknown $userId 
  * @return void
  * @access public
  */
 static function verify($key, $user_id, $auth_key_type_id, $foreign_id = null)
 {
     $_this = ClassRegistry::init('AuthKey');
     AuthKey::purgeExpired();
     if (!Common::isUuid($auth_key_type_id)) {
         $auth_key_type_id = $_this->AuthKeyType->lookup($auth_key_type_id, 'id', false);
     }
     if (!Common::isUuid($auth_key_type_id)) {
         return false;
     }
     $options = compact('key', 'user_id', 'auth_key_type_id');
     if (!empty($foreign_id)) {
         $options = compact('key', 'user_id', 'auth_key_type_id', 'foreign_id');
     }
     return $_this->hasAny($options);
 }
 /**
  * undocumented function
  *
  * @return void
  * @access public
  */
 function view($key)
 {
     $userId = $this->params['named']['user_id'];
     $authKeyTypeId = $this->params['named']['auth_key_type_id'];
     Assert::true(Common::isUuid($userId), '403');
     Assert::true(Common::isUuid($authKeyTypeId), '403');
     Assert::true(AuthKey::verify($key, $userId, $authKeyTypeId), '403');
     $authKeyType = $this->AuthKey->AuthKeyType->lookup(array('id' => $authKeyTypeId), 'name', false);
     User::login($userId);
     switch ($authKeyType) {
         case 'Lost Password':
             $this->Session->write('lost_password', true);
             $msg = __('Please go ahead and change your password now.', true);
             $this->Message->add($msg, 'ok', true, '/admin/users/edit_password/' . $userId);
     }
 }
Beispiel #6
0
 /**
  * undocumented function
  *
  * @param string $key
  * @param string $tId
  * @return void
  * @access public
  */
 function receipt($key, $tId)
 {
     $userId = $this->params['named']['user_id'];
     $authKeyTypeId = $this->params['named']['auth_key_type_id'];
     Assert::true(Common::isUuid($authKeyTypeId), '403');
     Assert::true(Common::isUuid($tId), '403');
     Assert::true(AuthKey::verify($key, $userId, $authKeyTypeId, $tId), '403');
     $transaction = $this->Transaction->find('first', array('conditions' => array('Transaction.id' => $tId), 'contain' => array('Gift.Contact.Address.Phone', 'Gift.Contact.Phone')));
     $this->set(compact('transaction'));
 }
Beispiel #7
0
 /**
  * Uses a given $user data array to make him the active user
  *
  * @param array $user A User data array as returned by User::read()
  * @return boolean True on success, false on failure
  * @access public
  */
 static function setActive($user = null, $updateSession = false, $generateAuthCookie = false)
 {
     $_this = ClassRegistry::init('User');
     if (Common::isUuid($user)) {
         $user = $_this->find('first', array('conditions' => array('User.id' => $user), 'contain' => array('Role(permissions, name)', 'Contact.Address.State(id, name)', 'Contact.Address.Country(id, name)', 'Contact.Address.City(id, name)'), 'fields' => array('User.id', 'User.name', 'User.login', 'User.password', 'User.permissions', 'User.active', 'User.tooltips', 'User.role_id', 'User.office_id', 'User.contact_id', 'User.lang')));
     }
     Assert::true(Common::isUuid($user['User']['id']), '500');
     Configure::write('User', $user);
     Assert::identical(Configure::read('User'), $user);
     if (!$updateSession && !$generateAuthCookie) {
         return true;
     }
     if (!User::is('guest') && isset($user['User']['office_id'])) {
         $_this->Office->activate($user['User']['office_id']);
     }
     $Session = Common::getComponent('Session');
     Assert::true($Session->write('User', $user));
     $Cookie = Common::getComponent('Cookie');
     $Cookie->write('Auth.name', $user['User']['login'], false, Configure::read('App.loginCookieLife'));
     // if ($user['User']['login'] != Configure::read('App.guestAccount')) {
     // 	$oldDomain = $Cookie->domain;
     // 	$Cookie->domain = '.greenpeace.org';
     // 	$Cookie->write('User.name', $user['User']['name'], false, Configure::read('App.loginCookieLife'));
     // 	$Cookie->write('User.country', $user['Address']['Country']['name'], false, Configure::read('App.loginCookieLife'));
     // 	$Cookie->domain = $oldDomain;
     // }
     if (!$generateAuthCookie) {
         return true;
     }
     $key = AuthKey::generate(array('auth_key_type_id' => 'Login Cookie'));
     $Cookie->write('Auth.key', $key, true, Configure::read('App.loginCookieLife'));
     return Assert::equal($Cookie->read('Auth.key'), $key);
 }
Beispiel #8
0
 /**
  * Set the user as current
  * @param array $user
  * @param bool $updateSession
  * @param bool $generateAuthCookie
  */
 static function setActive($user = null, $find = true)
 {
     $_this = Common::getModel('User');
     //@TODO only fetch if $user is incomplete compared to find conditions
     if ($find) {
         if ($user != 'guest' && isset($user['User']['id']) && Common::isUuid($user['User']['id'])) {
             $user = $_this->find('first', $_this->getFindOptions('userActivation', $user));
         }
         if ($user == 'guest' || is_null($user) || empty($user)) {
             $user = $_this->find('first', $_this->getFindOptions('guestActivation'));
         }
     }
     if (isset($user['User']['password'])) {
         unset($user['User']['password']);
         // just to make sure
     }
     Configure::write('User', $user);
     $Session = Common::getComponent('Session');
     $Session->write('User', $user);
     return $user;
 }
Beispiel #9
0
 /**
  * Returns if the parameter object belongs to the currently logged in user
  *
  * @param array $obj
  * @param string $model
  * @return boolean true if the $obj array contains a user_id key that is equal to User::get('id'), false if not
  * @access public
  */
 static function isOwn($obj, $model)
 {
     Assert::isArray($obj);
     if (isset($obj[$model]['user_id'])) {
         $userId = $obj[$model]['user_id'];
     } else {
         if (isset($obj['user_id'])) {
             $userId = $obj['user_id'];
         } else {
             $userId = User::get('id');
         }
     }
     if (!Common::isUuid($userId)) {
         return false;
     }
     return $userId == User::get('id');
 }