Exemple #1
0
 function editAction()
 {
     $request = new Bolts_Request($this->getRequest());
     $roles_table = new Roles();
     $role = null;
     if ($request->has('id')) {
         if (!is_null($request->id)) {
             $role = $roles_table->fetchRow($roles_table->select()->where("id = ?", $request->id));
             if (!is_null($role)) {
                 // we do not edit the guest role
                 if ($role->shortname == "guest") {
                     $this->_redirect("/bolts/role");
                 }
                 $this->view->role = $role->toArray();
                 $this->view->role_tree = $roles_table->getRoleTree(null, $role->id);
                 $this->view->inherited_ids = $roles_table->getInheritedRoles($role->id);
             }
         }
     }
     if (is_null($role)) {
         $this->view->role_tree = $roles_table->getRoleTree();
     }
     if ($this->getRequest()->isPost()) {
         $errors = array();
         if ($request->has('inherit_role')) {
             $parents = array();
             foreach ($request->inherit_role as $inherit_role) {
                 $parents = array_merge($parents, $roles_table->getAllAncestors($inherit_role));
             }
             $inherit_ids = array();
             foreach ($request->inherit_role as $inherit_role) {
                 if (!in_array($inherit_role, $parents)) {
                     $inherit_ids[] = $inherit_role;
                 }
             }
         }
         if ($request->has('shortname')) {
             $shortname = $request->shortname;
             if (!Bolts_Validate::checkLength($request->shortname, 1, 255)) {
                 $errors[] = $this->_T("Shortname must be between 1 and 255 chars.");
             }
         } else {
             $errors[] = $this->_T("Shortname is a requried field.");
         }
         $description = $request->description;
         $isadmin = (int) $request->checkbox('isadmin');
         if (count($errors) == 0) {
             $data = array('shortname' => $shortname, 'description' => $description, 'isadmin' => $isadmin);
             //If we have an id, this is an update.
             $id = (int) $this->_request->getPost('id');
             if ($id != 0) {
                 $where = 'id = ' . $id;
                 $roles_table->update($data, $where);
             } else {
                 //We don't, this is an insert.
                 $id = $roles_table->insert($data);
             }
             $roles_table->removeInheritedRole($id);
             foreach ($inherit_ids as $in_id) {
                 $roles_table->setInheritedRole($id, $in_id);
             }
             $this->_redirect("/bolts/role");
         } else {
             $this->view->errors = $errors;
         }
     }
     if ($request->has('id')) {
         // this is an edit
         $id = $request->id;
         if ($id > 0) {
             $this->view->role = $roles_table->fetchRow('id = ' . $id)->toArray();
         }
         $this->view->inherited_ids = $roles_table->getInheritedRoles($id);
     } else {
         foreach ($roles_table->fetchAll()->toArray() as $role) {
             $role_choices[$role['id']] = $role['shortname'];
         }
         $this->view->role_choices = $role_choices;
     }
 }
 function editAction()
 {
     $errors = array();
     $users_table = new Users();
     $users_roles_table = new UsersRoles();
     $request = new Bolts_Request($this->getRequest());
     $countries_table = new Countries();
     $this->view->countries = $countries_table->getCountriesArray('Choose a country...');
     $roles_table = new Roles();
     $roles = $roles_table->fetchAll(NULL, "shortname ASC");
     $arRoles = array();
     foreach ($roles as $role) {
         if (!strpos($role->shortname, "-base")) {
             $arRoles[$role->id] = $role->description;
         }
     }
     $this->view->roles = $arRoles;
     $is_new = true;
     $user = array();
     if ($request->has('username')) {
         $obUser = $users_table->fetchByUsername($request->username);
         if (!is_null($obUser)) {
             $is_new = false;
             $user_roles = $users_roles_table->fetchAll($users_roles_table->select()->where("username = ?", $obUser->username));
             if (count($user_roles) > 0) {
                 $tmp_selected = array();
                 foreach ($user_roles as $user_role) {
                     $tmp_selected[] = $user_role->role_id;
                 }
                 $this->view->selected_roles = $tmp_selected;
             }
             $user = $obUser->toArray();
         }
     }
     $this->view->is_new = $is_new;
     if ($is_new) {
         // defaults for form fields
         $user['username'] = "";
         $user['full_name'] = "";
         $user['aboutme'] = "";
     }
     $pre_render = $this->_Bolts_plugin->doFilter($this->_mca . "_pre_render", array('user' => $user, 'request' => $this->_request));
     // FILTER HOOK
     $user = $pre_render['user'];
     foreach ($pre_render as $key => $value) {
         if ($key != "user") {
             $this->view->{$key} = $value;
         }
     }
     // $tags = unserialize($user['tags']);
     if ($this->getRequest()->isPost()) {
         $errors = array();
         $request->stripTags(array('full_name', 'email', 'newpassword', 'confirm'));
         // $request->stripTags(array('full_name', 'email', 'newpassword', 'confirm', 'aboutme'));
         $user['username'] = $request->username;
         $user['email'] = $request->email;
         $user['password'] = $request->newpassword;
         $user['confirm'] = $request->confirm;
         $user['full_name'] = $request->full_name;
         $user['birthday'] = $birthday = strtotime($request->Birthday_Day . $request->Birthday_Month . $request->Birthday_Year);
         $user['gender'] = $request->gender;
         $user['country_code'] = $request->country_code;
         $user['aboutme'] = $request->aboutme;
         // validate username
         $username_validator = new Zend_Validate();
         $username_validator->addValidator(new Zend_Validate_StringLength(1, Bolts_Registry::get('username_length')));
         $username_validator->addValidator(new Zend_Validate_Alnum());
         if (!$username_validator->isValid($user['username'])) {
             $show_username = "******" . $user['username'] . "'";
             if (trim($user['username']) == "") {
                 $show_username = "******" . $this->_T("empty") . "]";
             }
             $errors[] = $this->_T("%s isn't a valid username. (Between %d and %d characters, only letters and numbers)", array($show_username, 1, Bolts_Registry::get('username_length')));
         }
         if ($is_new) {
             $user_where = $users_table->getAdapter()->quoteInto('username = ?', $user['username']);
             if ($users_table->getCountByWhereClause($user_where) > 0) {
                 $errors[] = $this->_T("The username '%s' is already in use", $user['username']);
             }
         }
         // validate email
         if (!Bolts_Validate::checkEmail($user['email'])) {
             $errors[] = $this->_T("Email is not valid");
         }
         // check to see if email is in use already by someone else
         if ($users_table->isEmailInUse($user['email'], $user['username'])) {
             $errors[] = $this->_T("Email already in use");
         }
         // if password isn't blank, validate it
         if ($user['password'] != "") {
             if (!Bolts_Validate::checkLength($user['password'], 6, Bolts_Registry::get('password_length'))) {
                 $errors[] = $this->_T("Password must be between 6 and 32 characters");
             }
             // if password is set, make sure it matches confirm
             if ($user['password'] != $user['confirm']) {
                 $errors[] = $this->_T("Passwords don't match");
             }
         }
         // convert birthday_ts to mysql date
         $birthday = date("Y-m-d H:i:s", $user['birthday']);
         $params = array('request' => $request, 'user' => $user, 'errors' => $errors);
         // upload new avatar image if present
         if (array_key_exists('filedata', $_FILES)) {
             if ($_FILES['filedata']['tmp_name'] != '') {
                 $destination_path = Bolts_Registry::get('upload_path') . "/" . $user['username'] . "/original";
                 if (!is_dir($destination_path)) {
                     mkdir($destination_path, 0777, true);
                     Bolts_Log::report("Creating user folder at " . $destination_path, null, Zend_Log::DEBUG);
                 }
                 if (file_exists($destination_path . "/avatar")) {
                     unlink($destination_path . "/avatar");
                     Bolts_Log::report("Deleted existing user avatar from " . $destination_path, null, Zend_Log::DEBUG);
                 } else {
                     Bolts_Log::report("User avatar did not exist in " . $destination_path, null, Zend_Log::DEBUG);
                 }
                 move_uploaded_file($_FILES['filedata']['tmp_name'], $destination_path . "/avatar");
                 Users::clearUserCache($user['username']);
                 Bolts_Log::report("User avatar uploaded to " . $destination_path, null, Zend_Log::DEBUG);
                 $params['user']['hasnewfile'] = true;
             } else {
                 $params['user']['hasnewfile'] = false;
             }
         }
         $additional = $this->_Bolts_plugin->doFilter($this->_mca . "_pre_save", $params);
         // FILTER HOOK
         $errors = $additional['errors'];
         $user = $additional['user'];
         $users_roles_table->delete($users_roles_table->getAdapter()->quoteInto("username = ?", $user['username']));
         foreach ($request->role_ids as $role_id) {
             $role_data = array("username" => $user['username'], "role_id" => $role_id);
             $users_roles_table->insert($role_data);
         }
         if (count($errors) == 0) {
             /**********  Commented out due to Plug-in compatibility issues. 
             			$data = array(
             				'email' => $user['email'],
             				'birthday' => $birthday,
             				'aboutme' => nl2br($user['aboutme']),
             				'gender' => $user['gender'],
             				'full_name' => $user['full_name'],
             				'country_code' => $user['country_code'],
             				'last_modified_on' => date(DB_DATETIME_FORMAT),
             			);
             			**********/
             $user['birthday'] = $birthday;
             $user['aboutme'] = nl2br($user['aboutme']);
             $user['last_modified_on'] = date(DB_DATETIME_FORMAT);
             // This is a hold-over value from the form.
             unset($user['confirm']);
             if ($user['password'] != "") {
                 #$data['password'] = $user['password'];
             } else {
                 unset($user['password']);
             }
             if ($is_new) {
                 // TODO - stuff?  really?
                 $stuff = array('request' => $request, 'user' => $user, 'errors' => $errors);
                 $additional1 = $this->_Bolts_plugin->doFilter($this->_mca, $stuff);
                 // FILTER HOOK
                 $errors = $additional1['errors'];
                 $user = $additional1['user'];
                 $data['username'] = $user['username'];
                 #$data['created_on'] = date(DB_DATETIME_FORMAT);
                 $user['created_on'] = date(DB_DATETIME_FORMAT);
                 $users_table->insert($user);
                 $this->view->success = "Profile created.";
             } else {
                 $where = $users_table->getAdapter()->quoteInto('username = ?', $user['username']);
                 #$users_table->update($data, $where);
                 $users_table->update($user, $where);
                 $this->view->success = "Profile updated.";
             }
         } else {
             $this->view->errors = $errors;
         }
     }
     $this->view->end_year = -Bolts_Registry::get('minimum_registration_age');
     $this->view->genders = Bolts_Common::getGenderArray();
     $user['aboutme'] = Bolts_Common::br2nl($user['aboutme']);
     $this->view->user = $user;
 }
Exemple #3
0
 function findRolesCandidats($annee)
 {
     $t = new Roles();
     $db = $t->getAdapter();
     $s = $t->select()->setIntegrityCheck(false)->from('unite_role')->join('unite_type', 'unite_type.id = unite_role.type', array())->join('unite', 'unite.type = unite_type.id', array())->joinLeft('appartenance', 'appartenance.role = unite_role.id AND ' . 'appartenance.unite = unite.id AND ' . ('(' . $db->quoteInto('appartenance.debut < ?', Strass_Controller_Action_Helper_Annee::dateFin($annee)) . ' AND ' . $db->quoteInto('(appartenance.fin IS NULL OR appartenance.fin < ?)', Strass_Controller_Action_Helper_Annee::dateFin($annee)) . ')'), array())->where('unite.id = ?', $this->id)->where('appartenance.id IS NULL');
     return $t->fetchAll($s);
 }
Exemple #4
0
 function findRolesCandidats($unite, $filter_current = true)
 {
     $t = new Roles();
     $db = $t->getAdapter();
     $s = $t->select()->setIntegrityCheck(false)->from('unite_role')->join('unite_type', 'unite_type.id = unite_role.type', array())->join('unite', 'unite.type = unite_type.id', array())->where('unite.id = ?', $unite->id);
     if ($filter_current) {
         $s->joinLeft('appartenance', 'appartenance.role = unite_role.id AND ' . 'appartenance.unite = unite.id AND ' . $db->quoteInto('appartenance.individu', $this->id), array())->where('appartenance.id IS NULL');
     }
     return $t->fetchAll($s);
 }
 function adminAction()
 {
     $this->view->individu = $individu = $this->_helper->Individu();
     $this->assert(null, $individu, 'admin', "Vous n'avez pas le droit d'administrer " . "l'inscription de cet individu.");
     $this->metas(array('DC.Title' => 'Administrer ' . $individu->getFullname()));
     $this->actions->append("Éditer la fiche", array('controller' => 'individus', 'action' => 'editer'), array(null, $individu));
     $as = $individu->findAppartenances(null, 'debut DESC');
     if (!$as->count()) {
         $this->view->apps = null;
     } else {
         $this->view->apps = $m = new Wtk_Form_Model('apps');
         $tu = new Unites();
         $us = $tu->fetchAll(null);
         $eu = array();
         foreach ($us as $u) {
             $eu[$u->id] = mb_substr($u->getFullName(), 0, 32);
         }
         $tr = new Roles();
         $rs = $tr->fetchAll(null, 'ordre');
         $er = array();
         foreach ($rs as $r) {
             $er[$r->id] = substr($r->slug, 0, 7);
         }
         $i = $m->addTable('appartenances', "Appartenances", array('unite' => array('Enum', 'Unité', $eu), 'role' => array('Enum', 'Role', $er), 'titre' => array('String', 'Titre'), 'debut' => array('Date', 'Début'), 'clore' => array('Bool', 'Clore', false), 'fin' => array('Date', 'Fin')));
         foreach ($as as $a) {
             $i->addRow($a->unite, $a->role, $a->titre, $a->debut, (bool) $a->fin, $a->fin);
         }
         $m->addNewSubmission('enregistrer', 'Enregistrer');
         if ($m->validate()) {
             $t = new Appartenances();
             $db = $t->getAdapter();
             $db->beginTransaction();
             try {
                 foreach ($as as $a) {
                     $a->delete();
                 }
                 foreach ($i as $row) {
                     $data = array('individu' => $individu->id, 'unite' => $row->unite, 'role' => $row->role, 'titre' => $row->titre, 'debut' => $row->debut);
                     if ($row->clore) {
                         $data['fin'] = $row->fin;
                     } else {
                         $data['fin'] = null;
                     }
                     $t->insert($data);
                 }
                 $this->logger->info("Inscription éditée", $this->_helper->Url('fiche', 'individus', null, array('individu' => $individu->slug), true));
                 $db->commit();
             } catch (Exception $e) {
                 $db->rollBack();
                 throw $e;
             }
             $this->redirectSimple('fiche', 'individus', null, array('individu' => $individu->slug));
         }
     }
 }
Exemple #6
0
 public function browseAction()
 {
     $form = new Modules_Admin_Controllers_Users_Browse_Form();
     $cache = Zend_Registry::get('cache');
     $roles = $cache->load(md5(UNIQUE_HASH . 'roles'));
     if ($roles === false) {
         $rolesTable = new Roles();
         $roles = $rolesTable->fetchAll();
         $cache->save($roles, md5(UNIQUE_HASH . 'roles'));
     }
     foreach ($roles as $role) {
         if ($role->id != 1) {
             $form->getElement('role')->addMultiOption($role->id, $role->name);
         }
     }
     $router = $this->getFrontController()->getRouter();
     if ($page = (int) $this->getRequest()->getParam('page')) {
         $router->setGlobalParam('page', $page);
     }
     if ($belongs = (int) $this->getRequest()->getParam('belongs')) {
         $router->setGlobalParam('belongs', $belongs);
     }
     if ($role = $this->getRequest()->getParam('role')) {
         $router->setGlobalParam('role', $role);
     }
     if ($order = $this->getRequest()->getParam('order')) {
         $router->setGlobalParam('order', $order);
     }
     if ($sort = $this->getRequest()->getParam('sort')) {
         $router->setGlobalParam('sort', $sort);
     }
     $form->getElement('role')->setValue($role);
     $form->getElement('belongs')->setValue($belongs);
     $this->view->form = $form;
     $usersTable = new Users();
     #fetching the data from table
     $select = $usersTable->select()->from(array('u' => 'users'));
     $select->setIntegrityCheck(false);
     $select->where('u.id > ?', 1);
     if ($role) {
         if ($belongs) {
             $select->join(array('ur' => 'users_roles'), 'ur.user=u.id AND ur.role=' . (int) $role);
         } else {
             $select->joinLeft(array('ur' => 'users_roles'), 'ur.user=u.id AND ur.role=' . (int) $role);
             $select->where('ur.role is null');
         }
     }
     switch ($sort) {
         case 'desc':
             $sort = 'DESC';
             break;
         default:
             $sort = 'ASC';
     }
     switch ($order) {
         case 'username':
             $select->order('u.email ' . $sort);
             break;
         case 'email':
             $select->order('u.email ' . $sort);
             break;
         case 'dateOfRegistration':
             $select->order('u.registered_timestamp ' . $sort);
             break;
     }
     $this->view->usersPaginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
     $this->view->usersPaginator->setItemCountPerPage(50);
     $this->view->usersPaginator->setCurrentPageNumber($page);
 }