diff --git a/class/UserRight.class.php b/class/UserRight.class.php index c30185e..9d88006 100644 --- a/class/UserRight.class.php +++ b/class/UserRight.class.php @@ -9,6 +9,7 @@ use \Payutc\Db\Dbal; use \Payutc\Db\DbBuckutt; +use \Payutc\Bom\User; class UserRight { protected $db; @@ -108,7 +109,10 @@ 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); @@ -116,9 +120,9 @@ public static function setRight($usr_id, $service, $fun_id) { 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."); } diff --git a/src/Payutc/Bom/User.php b/src/Payutc/Bom/User.php index 1df6e3b..02a53f8 100644 --- a/src/Payutc/Bom/User.php +++ b/src/Payutc/Bom/User.php @@ -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']; + } } diff --git a/tests/UserRightRodbTest.php b/tests/UserRightRodbTest.php new file mode 100644 index 0000000..7f66b3e --- /dev/null +++ b/tests/UserRightRodbTest.php @@ -0,0 +1,27 @@ +computeDataset(array( + 'users.yml' + )); + } + + /** + * @expectedException \Payutc\Exception\SetRightException + * @expectedExceptionMessage User #99942 does not exist + */ + public function testSetRightOnNoExistingUserThrowException() + { + UserRight::setRight(99942, 0, 0); + } +} + diff --git a/tests/UserRodbTest.php b/tests/UserRodbTest.php index 538628e..f2a0425 100644 --- a/tests/UserRodbTest.php +++ b/tests/UserRodbTest.php @@ -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())); + } }