示例#1
0
文件: sql_role.php 项目: cepharum/txf
 /**
  * Makes user adopting current role.
  *
  * @param user $user user to adopt current role
  * @return $this
  * @throws \Exception on failing to adopt role
  */
 public function makeAdoptedBy(user $user)
 {
     $role = $this;
     if (!$this->_source->transaction()->wrap(function (datasource\connection $db) use($role, $user) {
         $userID = $user->getUUID();
         $roleID = $role->id;
         $count = $db->createQuery('user_role')->addCondition('user_uuid=?', true, $userID)->addCondition('role_id=?', true, $roleID)->execute(true)->cell();
         if ($count > 0) {
             // role has been adopted before
             return true;
         }
         $qSet = $db->qualifyDatasetName('user_role');
         return $db->test('INSERT INTO ' . $qSet . ' (user_uuid,role_id) VALUES (?,?)', $userID, $roleID) !== false;
     })) {
         throw new \RuntimeException(sprintf('adopting role %s by user %s failed', $this->label, $user->getName()));
     }
     return $this;
 }