/** * This method adds/saves a user * * @param $id If we are editing, $id is the user id. If we are adding, $id is the parent_id * @param $edit Boolean flag that defines if we are editing $id or adding to $id * @param $formvalues (Optional) Custom array with user attributes * * @returns YDResult */ function _saveFormDetails($id, $edit, $formvalues = null) { // check form validation if (!$this->_form->validate($formvalues)) { return YDResult::warning(t('form errors'), $this->_form->getErrors()); } // get form values EXCLUDING spans $values = $this->_form->getValues(); // check if we are editing or adding an element if ($edit) { // create userobject node $userobject = array(); $userobject['type'] = 'YDCMUser'; if (isset($values['name'])) { $userobject['reference'] = $values['name']; } if (isset($values['state'])) { $userobject['state'] = $values['state']; } // update userobject $uobj = new YDCMUserobject(); $res = $uobj->updateNode($userobject, $id); // create user row $user = array(); if (isset($values['password'])) { $user['password'] = md5($values['password']); } if (isset($values['username'])) { $user['username'] = $values['username']; } if (isset($values['name'])) { $user['name'] = $values['name']; } if (isset($values['email'])) { $user['email'] = $values['email']; } if (isset($values['other'])) { $user['other'] = $values['other']; } if (isset($values['lang_id'])) { $user['lang_id'] = $values['lang_id']; } if (isset($values['template'])) { $user['template'] = $values['template']; } // check login schedule dates if (isset($user['login_start'])) { $user['login_start'] = YDStringUtil::formatDate($values['login_start'], 'datetimesql'); } if (isset($user['login_end'])) { $user['login_end'] = YDStringUtil::formatDate($values['login_end'], 'datetimesql'); } // update user $this->resetValues(); $this->setValues($user); $this->where('user_id = ' . $id); $res = $this->update(); // check update result and return if ($res > 0) { return YDResult::ok(t('ydcmuser mess updated'), $res); } else { return YDResult::warning(t('ydcmuser mess impossible to update'), $res); } } else { // check if parent it is set in argument or was choosen in the group selectbox if (is_null($id)) { $id = $values['group']; } // create userobject node $userobject = array(); $userobject['type'] = 'YDCMUser'; $userobject['reference'] = $values['name']; $userobject['state'] = isset($values['state']) ? $values['state'] : 0; // insert a new node in userobject and get the new id for user row creation $uobj = new YDCMUserobject(); $res = $uobj->addNode($userobject, intval($id)); // create user row $user = array(); // add REQUIRED values $user['user_id'] = $res; $user['password'] = md5($values['password']); $user['username'] = $values['username']; $user['lang_id'] = $values['lang_id']; $user['template'] = $values['template']; $user['name'] = isset($values['name']) ? $values['name'] : ''; $user['email'] = isset($values['email']) ? $values['email'] : ''; $user['other'] = isset($values['other']) ? $values['other'] : ''; $user['login_counter'] = 0; $user['login_start'] = YDStringUtil::formatDate(0, 'datetimesql'); $user['login_end'] = YDStringUtil::formatDate(0, 'datetimesql'); $user['login_last'] = YDStringUtil::formatDate(0, 'datetimesql'); $user['login_current'] = YDStringUtil::formatDate(0, 'datetimesql'); // reset object $this->resetValues(); $this->setValues($user); // insert values if ($this->insert()) { return YDResult::ok(t('ydcmuser mess created'), $res); } else { return YDResult::fatal(t('ydcmuser mess impossible to create'), $res); } } }
/** * This method adds/saves a permission system * * @param $id If we are editing, $id is the group id. If we are adding, $id is the user id * @param $edit Boolean flag that defines if we are editing $id or adding to $id * @param $formvalues (Optional) Custom array with user attributes * @param $group_id Node id used when inserting permissions. nodeID is the node where permissions are applyed * * @returns INT: total of rows affected */ function _saveFormDetails($id, $edit, $formvalues = null, $group_id = 0) { // validate with custom values if (!is_null($formvalues)) { $this->_form->validate($formvalues); } // get form values EXCLUDING spans $values = $this->_form->getValues(); // if this group is not a root group we must get the parent group permissions to check the ones we can use $userobject = new YDCMUserobject(); $groups = $userobject->getElements(array('ydcmgroup', 'ydcmuser')); $parent_id = $groups[$id]['parent_id']; // when adding a new group, parent of $id is the master group. when editing a group, parent is a user.. so the master group is the parent of the parent if ($edit == true) { $parent_id = $groups[$parent_id]['parent_id']; } // if parent of this group is root, parentgroup permissions are ALL (read: null), otherwise we must get permissions of that parent if ($parent_id == 1) { $parentgroup_perms = null; } else { $parentgroup_perms = $this->getPermissions($parent_id); } // if we are editing, we must get the current group permissions. if we are adding, current permissions are empty if ($edit == true) { $perms = $this->getPermissions($id); } else { $perms = array(); } $actions_to_add = array(); $actions_to_del = array(); // get all possible actions to compute actions we must add and actions we must delete foreach ($this->getRegisteredActions() as $class => $actions) { foreach ($actions as $action) { // if action is selected by the user AND // this is a root group OR the action belogs to the parent group // we can add it if (isset($values['pclass_' . $class][$action]) && ($values['pclass_' . $class][$action] == 1 || $values['pclass_' . $class][$action] == 'on')) { // check if action is valid: // if parent group is a root a group OR the parent group has this action if (is_null($parentgroup_perms) || isset($parentgroup_perms[$class][$action])) { // if action is valid we must check if we must add it or the user already has it if (!isset($perms[$class][$action])) { $actions_to_add[] = array($class, $action); } continue; } // if action selected is not valid we must delete it $actions_to_del[] = array($class, $action); } // if action is not set, we will always delete it (even if is not in bd) $actions_to_del[] = array($class, $action); } } // if we are adding permissions we must add to group_id recently created (not to parent) if ($edit == false) { $id = $group_id; } $rows_deleted = 0; // delete actions and count rows affected foreach ($actions_to_del as $ac) { $this->resetValues(); $this->set('permission_id', $id); $this->set('class', $ac[0]); $this->set('action', $ac[1]); $rows_deleted += $this->delete(); } $rows_added = 0; // add actions and count total of action added foreach ($actions_to_add as $ac) { $this->resetValues(); $this->set('permission_id', $id); $this->set('class', $ac[0]); $this->set('action', $ac[1]); $this->insert(); $rows_added++; } // TODO: currently YDDatabaseObject don't have a mechanism to control the above deletes and inserts return $rows_deleted + $rows_added; }
/** * This method adds/saves a group * * @param $id If we are editing, $id is the group id. If we are adding, $id is the parent_id * @param $edit Boolean flag that defines if we are editing $id or adding to $id * @param $formvalues (Optional) Custom array with attributes * * @returns YDResult */ function _saveFormDetails($id, $edit, $formvalues = null) { // check form validation if (!$this->_form->validate($formvalues)) { return YDResult::warning(t('form errors'), $this->_form->getErrors()); } // get form values EXCLUDING spans $values = $this->_form->getValues(); // check if we are editing or adding an element if ($edit) { // create userobject node $userobject = array(); $userobject['type'] = 'YDCMGroup'; $userobject['reference'] = $values['name']; $userobject['state'] = 1; // update userobject $uobj = new YDCMUserobject(); $res = $uobj->updateNode($userobject, $id); // create user row $group = array(); $group['name'] = $values['name']; $group['description'] = $values['description']; // update user $this->resetValues(); $this->setValues($group); $this->where('group_id = ' . $id); // update and sum lines afected to userobject $res += $this->update(); // if we are using the permission system, update and sum lines afected in permission table if (isset($this->editing_PERMOBJ)) { $res += $this->editing_PERMOBJ->saveFormEdit($id, $formvalues); } // check update from node update and from group update if ($res > 0) { return YDResult::ok(t('ydcmgroup mess details updated'), $res); } else { return YDResult::warning(t('ydcmgroup mess details not updated'), $res); } } else { // create userobject node $userobject = array(); $userobject['type'] = 'YDCMGroup'; $userobject['reference'] = $values['name']; $userobject['state'] = 1; // check default parent id if (is_null($id)) { $id = 1; } // TODO: check if group is valid (and, eg, is not a user node) // update userobject and get new id $uobj = new YDCMUserobject(); $nodeID = $uobj->addNode($userobject, intval($id)); // init result count $res = $nodeID; // if we are using the permission system, add permissions and sum lines afected in permission table if (isset($this->editing_PERMOBJ)) { $res += $this->editing_PERMOBJ->saveFormNew($id, $nodeID, $formvalues); } // create user row $group = array(); $group['group_id'] = intval($nodeID); $group['name'] = $values['name']; $group['description'] = $values['description']; // reset object $this->resetAll(); $this->setValues($group); // insert values if ($this->insert()) { return YDResult::ok(t('ydcmgroup mess created'), $res); } else { return YDResult::fatal(t('ydcmgroup mess impossible to create'), $res); } } }