Skip to content

Commit

Permalink
Right can't be added on inexisting user
Browse files Browse the repository at this point in the history
Fix #201
  • Loading branch information
trecouvr committed Sep 17, 2013
1 parent b9a7ced commit b7b6c90
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
10 changes: 7 additions & 3 deletions class/UserRight.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use \Payutc\Db\Dbal;
use \Payutc\Db\DbBuckutt;
use \Payutc\Bom\User;

class UserRight {
protected $db;
Expand Down Expand Up @@ -108,17 +109,20 @@ public static function getRights($fun_id) {
* Donne les droits à un user sur un service et une fundation
*/
public static function setRight($usr_id, $service, $fun_id) {
$allready_set = true;
if (!User::userExistById($usr_id)) {
throw new \Payutc\Exception\SetRightException("User #$usr_id does not exist");
}
$already_set = true;
try {
if($fun_id) {
static::check($usr_id, $service, true, $fun_id);
} else {
static::check($usr_id, $service, false);
}
} catch (Exception $e) {
$allready_set = false;
$already_set = false;
}
if($allready_set) {
if ($already_set) {
throw new \Payutc\Exception\RightAlreadyExistsException("L'utilisateur à déjà ce droit.");
}

Expand Down
10 changes: 10 additions & 0 deletions src/Payutc/Bom/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,4 +448,14 @@ public static function createAndGetNewUser($login) {
// Return a User object
return new User($gingerUser->login, $gingerUser);
}

public static function userExistById($id) {
$qb = Dbal::createQueryBuilder();
$q = $qb->select('count(*) as count')
->from('ts_user_usr', 'usr')
->where('usr_id = :id')
->setParameter('id', $id);
$res = $q->execute()->fetch();
return 0 != $res['count'];
}
}
27 changes: 27 additions & 0 deletions tests/UserRightRodbTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

require_once 'bootstrap.php';


class UserRightRodbTest extends ReadOnlyDatabaseTest
{
/**
* get db dataset
*/
public function getDataSet()
{
return $this->computeDataset(array(
'users.yml'
));
}

/**
* @expectedException \Payutc\Exception\SetRightException
* @expectedExceptionMessage User #99942 does not exist
*/
public function testSetRightOnNoExistingUserThrowException()
{
UserRight::setRight(99942, 0, 0);
}
}

7 changes: 7 additions & 0 deletions tests/UserRodbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,12 @@ public function testCheckUsrNotBlockedWithBlockedUsr_2()
$u = new User("mguffroy");
$u->checkNotBlocked(1);
}

public function testUserExist()
{
$this->assertFalse(User::userExistById(99942));
$u = new User("trecouvr");
$this->assertTrue(User::userExistById($u->getId()));
}
}

0 comments on commit b7b6c90

Please sign in to comment.