public function save($commit = true) { if (!$this->isValid()) { throw new Exception(__('Cannot save the model from an invalid form.')); } // remove all the permissions $perm = Pluf_Permission::getFromString('IDF.project-authorized-user'); $cm = $this->project->getMembershipData(); $guser = new Pluf_User(); foreach ($cm['authorized'] as $user) { Pluf_RowPermission::remove($user, $this->project, $perm); } if ($this->cleaned_data['private_project']) { foreach (preg_split("/\r\n|\r|\n|\\,/", $this->cleaned_data['authorized_users'], -1, PREG_SPLIT_NO_EMPTY) as $login) { $sql = new Pluf_SQL('login=%s', array(trim($login))); $users = $guser->getList(array('filter' => $sql->gen())); if ($users->count() == 1) { Pluf_RowPermission::add($users[0], $this->project, $perm); } } $this->project->private = 1; } else { $this->project->private = 0; } $this->project->update(); $this->project->membershipsUpdated(); }
/** * Validate the key. */ public function clean_key() { $this->cleaned_data['key'] = trim($this->cleaned_data['key']); $error = __('We are sorry but this confirmation key is not valid. Maybe you should directly copy/paste it from your confirmation email.'); if (false === ($email_id = self::checkKeyHash($this->cleaned_data['key']))) { throw new Pluf_Form_Invalid($error); } $guser = new Pluf_User(); $sql = new Pluf_SQL('email=%s AND id=%s', $email_id); if ($guser->getCount(array('filter' => $sql->gen())) != 1) { throw new Pluf_Form_Invalid($error); } return $this->cleaned_data['key']; }
/** * Validate the key. */ public function clean_key() { $this->cleaned_data['key'] = trim($this->cleaned_data['key']); $error = __('We are sorry but this validation key is not valid. Maybe you should directly copy/paste it from your validation email.'); if (false === ($cres = IDF_Form_PasswordInputKey::checkKeyHash($this->cleaned_data['key']))) { throw new Pluf_Form_Invalid($error); } $guser = new Pluf_User(); $sql = new Pluf_SQL('email=%s AND id=%s', array($cres[0], $cres[1])); if ($guser->getCount(array('filter' => $sql->gen())) != 1) { throw new Pluf_Form_Invalid($error); } if (time() - $cres[2] > 86400) { throw new Pluf_Form_Invalid(__('Sorry, but this verification key has expired, please restart the password recovery sequence. For security reasons, the verification key is only valid 24h.')); } return $this->cleaned_data['key']; }
/** * Just a simple control. */ public function clean_key() { $this->cleaned_data['key'] = trim($this->cleaned_data['key']); $error = __('We are sorry but this confirmation key is not valid. Maybe you should directly copy/paste it from your confirmation email.'); if (false === ($email_id = IDF_Form_RegisterInputKey::checkKeyHash($this->cleaned_data['key']))) { throw new Pluf_Form_Invalid($error); } $guser = new Pluf_User(); $sql = new Pluf_SQL('email=%s AND id=%s', $email_id); $users = $guser->getList(array('filter' => $sql->gen())); if ($users->count() != 1) { throw new Pluf_Form_Invalid($error); } if ($users[0]->active) { throw new Pluf_Form_Invalid(__('This account has already been confirmed. Maybe should you try to recover your password using the help link.')); } $this->_user_id = $email_id[1]; return $this->cleaned_data['key']; }
/** * Create 2 projects to work with and 2 users. */ public function setUp() { $this->projects = array(); $this->users = array(); for ($i = 1; $i < 3; $i++) { $project = new IDF_Project(); $project->name = 'Test project ' . $i; $project->shortname = 'test' . $i; $project->description = sprintf('This is a test project %d.', $i); $project->create(); $this->projects[] = $project; $user = new Pluf_User(); $user->last_name = 'user' . $i; $user->login = '******' . $i; $user->email = 'user' . $i . '@example.com'; $user->create(); $this->users[] = $user; } }
public function clean_login() { $this->cleaned_data['login'] = mb_strtolower(trim($this->cleaned_data['login'])); if (preg_match('/[^a-z0-9]/', $this->cleaned_data['login'])) { throw new Pluf_Form_Invalid(sprintf(__('The login "%s" can only contain letters and digits.'), $this->cleaned_data['login'])); } $guser = new Pluf_User(); $sql = new Pluf_SQL('login=%s', $this->cleaned_data['login']); if ($guser->getCount(array('filter' => $sql->gen())) > 0) { throw new Pluf_Form_Invalid(sprintf(__('The login "%s" is already used, please find another one.'), $this->cleaned_data['login'])); } return $this->cleaned_data['login']; }
/** * The update of the memberships is done in different places. This * avoids duplicating code. * * @param IDF_Project The project * @param array The new memberships data in 'owners' and 'members' keys */ public static function updateMemberships($project, $cleaned_data) { // remove all the permissions $cm = $project->getMembershipData(); $def = array('owners' => Pluf_Permission::getFromString('IDF.project-owner'), 'members' => Pluf_Permission::getFromString('IDF.project-member')); $guser = new Pluf_User(); foreach ($def as $key => $perm) { foreach ($cm[$key] as $user) { Pluf_RowPermission::remove($user, $project, $perm); } foreach (preg_split("/\r\n|\r|\n|\\,/", $cleaned_data[$key], -1, PREG_SPLIT_NO_EMPTY) as $login) { $sql = new Pluf_SQL('login=%s', array(trim($login))); $users = $guser->getList(array('filter' => $sql->gen())); if ($users->count() == 1) { Pluf_RowPermission::add($users[0], $project, $perm); } } } }
function clean_email() { $this->cleaned_data['email'] = mb_strtolower(trim($this->cleaned_data['email'])); $guser = new Pluf_User(); $sql = new Pluf_SQL('email=%s AND id!=%s', array($this->cleaned_data['email'], $this->user->id)); if ($guser->getCount(array('filter' => $sql->gen())) > 0) { throw new Pluf_Form_Invalid(sprintf(__('The email "%s" is already used.'), $this->cleaned_data['email'])); } return $this->cleaned_data['email']; }
/** * Based on the given string, try to find the matching user. * * Search order is: email, login, last_name. * * If no user found, simply returns null. * * @param string User * @return Pluf_User or null */ public static function findUser($string) { $string = trim($string); if (strlen($string) == 0) { return null; } $guser = new Pluf_User(); foreach (array('email', 'login', 'last_name') as $what) { $sql = new Pluf_SQL($what . '=%s', $string); $users = $guser->getList(array('filter' => $sql->gen())); if ($users->count() > 0) { return $users[0]; } } return null; }
public function testRowPermission() { $user = new Pluf_User(1); $group = new Pluf_Group(); $group->name = 'testRowPermission'; $group->description = 'testRowPermission'; $group->create(); for ($i = 1; $i <= 5; $i++) { $mess = new Pluf_Message(); $mess->user = $user; $mess->message = 'Dummy object to test against: ' . $i; $mess->create(); } $perm = new Pluf_Permission(); $perm->application = 'Pluf_RowPermission'; $perm->code_name = 'test1'; $perm->name = 'test1'; $perm->description = 'test1'; $perm->create(); // Permission through group $mess = new Pluf_Message(1); Pluf_RowPermission::add($group, $mess, $perm); $this->assertEquals(false, $user->hasPerm('Pluf_RowPermission.test1', $mess)); $user->setAssoc($group); $user->getAllPermissions(true); //reset the cache $this->assertEquals(true, $user->hasPerm('Pluf_RowPermission.test1', $mess)); $user->delAssoc($group); $user->getAllPermissions(true); //reset the cache $this->assertEquals(false, $user->hasPerm('Pluf_RowPermission.test1', $mess)); $user->setAssoc($group); $user->getAllPermissions(true); //reset the cache $this->assertEquals(true, $user->hasPerm('Pluf_RowPermission.test1', $mess)); Pluf_RowPermission::remove($group, $mess, $perm); $user->getAllPermissions(true); //reset the cache $this->assertEquals(false, $user->hasPerm('Pluf_RowPermission.test1', $mess)); // Permission through direct user Pluf_RowPermission::add($user, $mess, $perm); $user->getAllPermissions(true); //reset the cache $this->assertEquals(true, $user->hasPerm('Pluf_RowPermission.test1', $mess)); Pluf_RowPermission::remove($user, $mess, $perm); $user->getAllPermissions(true); //reset the cache $this->assertEquals(false, $user->hasPerm('Pluf_RowPermission.test1', $mess)); // Using string for the permission. Pluf_RowPermission::add($user, $mess, 'Pluf_RowPermission.test1'); $user->getAllPermissions(true); //reset the cache $this->assertEquals(true, $user->hasPerm('Pluf_RowPermission.test1', $mess)); Pluf_RowPermission::remove($user, $mess, 'Pluf_RowPermission.test1'); $user->getAllPermissions(true); //reset the cache $this->assertEquals(false, $user->hasPerm('Pluf_RowPermission.test1', $mess)); }
/** * Save the model in the database. * * @param bool Commit in the database or not. If not, the object * is returned but not saved in the database. * @return Object Model with data set from the form. */ function save($commit = true) { if (!$this->isValid()) { throw new Exception(__('Cannot save the model from an invalid form.')); } $user = new Pluf_User(); $user->first_name = '---'; // with both this set and // active==false we can find later // on, all the unconfirmed accounts // that could be purged. $user->last_name = $this->cleaned_data['login']; $user->login = $this->cleaned_data['login']; $user->email = $this->cleaned_data['email']; $user->language = $this->request->language_code; $user->active = false; $user->create(); self::sendVerificationEmail($user); return $user; }