/** * Save data * * @return boolean */ public function save() { // Trigger the onUserBeforeSave event. $data = $this->toArray(); $isNew = $this->isNew(); // Allow an exception to be thrown. try { $oldUser = self::oneOrNew($this->get('id')); // Trigger the onUserBeforeSave event. $result = Event::trigger('user.onUserBeforeSave', array($oldUser->toArray(), $isNew, $data)); if (in_array(false, $result, true)) { // Plugin will have to raise its own error or throw an exception. return false; } // Get any set access groups $groups = null; if ($this->hasAttribute('accessgroups')) { $groups = $this->get('accessgroups'); $this->removeAttribute('accessgroups'); } // Save record $result = parent::save(); if (!$result) { throw new Exception($this->getError()); } // Update access groups if ($groups && is_array($groups)) { Map::destroyByUser($this->get('id')); Map::addUserToGroup($this->get('id'), $groups); } // In case it's a new user, we need to grab the ID $data['id'] = $this->get('id'); // Fire the onUserAfterSave event Event::trigger('user.onUserAfterSave', array($data, $isNew, $result, $this->getError())); $this->purgeCache(); } catch (Exception $e) { $this->addError($e->getMessage()); $result = false; } return $result; }