protected function createAdminUserAndLogin() { $contestant = $this->createUserAndLogin(); $userRoles = new UserRoles(array('user_id' => $contestant->getUserId(), 'role_id' => ADMIN_ROLE, 'contest_id' => 0)); UserRolesDAO::save($userRoles); return $contestant; }
public function testAddContestAdmin() { // Get a contest $contestData = ContestsFactory::createContest(); // Get a user $user = UserFactory::createUser(); // Prepare request $r = new Request(); $r['auth_token'] = $this->login($contestData['director']); $r['usernameOrEmail'] = $user->getUsername(); $r['contest_alias'] = $contestData['request']['alias']; // Call api $response = ContestController::apiAddAdmin($r); // Get the role $contest = $contestData['contest']; $ur = UserRolesDAO::getByPK($user->getUserId(), CONTEST_ADMIN_ROLE, $contest->getContestId()); $this->assertNotNull($ur); }
/** * Get list of contests where the user has admin priviledges * * @param Request $r * @return string * @throws InvalidDatabaseOperationException */ public static function apiContests(Request $r) { self::authenticateRequest($r); $response = array(); $response['contests'] = array(); try { $contest_director_key = new Contests(array('director_id' => $r['current_user_id'])); $contests_director = ContestsDAO::search($contest_director_key); foreach ($contests_director as $contest) { $response['contests'][] = $contest->asArray(); } $contest_admin_key = new UserRoles(array('user_id' => $r['current_user_id'], 'role_id' => CONTEST_ADMIN_ROLE)); $contests_admin = UserRolesDAO::search($contest_admin_key); foreach ($contests_admin as $contest_key) { $contest = ContestsDAO::getByPK($contest_key->getContestId()); if (is_null($contest)) { self::$log->error("UserRoles has a invalid contest: {$contest->getContestId()}"); continue; } $response['contests'][] = $contest->asArray(); } usort($response['contests'], function ($a, $b) { return $a['contest_id'] > $b['contest_id'] ? -1 : 1; }); } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } $response['status'] = 'ok'; return $response; }
/** * Creates a new user and elevates his priviledges * * @param string $username * @param string $password * @param string $email * @return User */ public static function createAdminUser($username = null, $password = null, $email = null) { $user = self::createUser(); $userRoles = new UserRoles(array('user_id' => $user->getUserId(), 'role_id' => ADMIN_ROLE, 'contest_id' => 0)); UserRolesDAO::save($userRoles); return $user; }
/** * Returns all contest administrators * * @param Request $r * @return array * @throws InvalidDatabaseOperationException */ public static function apiAdmins(Request $r) { // Authenticate request self::authenticateRequest($r); Validators::isStringNonEmpty($r["contest_alias"], "contest_alias"); try { $contest = ContestsDAO::getByAlias($r["contest_alias"]); } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } if (!Authorization::IsContestAdmin($r["current_user_id"], $contest)) { throw new ForbiddenAccessException(); } $response = array(); $response["admins"] = UserRolesDAO::getContestAdmins($contest); $response["status"] = "ok"; return $response; }
/** * Returns all problem administrators * * @param Request $r * @return array * @throws InvalidDatabaseOperationException */ public static function apiAdmins(Request $r) { // Authenticate request self::authenticateRequest($r); Validators::isStringNonEmpty($r['problem_alias'], 'problem_alias'); try { $problem = ProblemsDAO::getByAlias($r['problem_alias']); } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } if (!Authorization::IsProblemAdmin($r['current_user_id'], $problem)) { throw new ForbiddenAccessException(); } $response = array(); $response['admins'] = UserRolesDAO::getProblemAdmins($problem); $response['group_admins'] = GroupRolesDAO::getProblemAdmins($problem); $response['status'] = 'ok'; return $response; }
public static function IsSystemAdmin($user_id) { if (self::$is_system_admin == null) { self::$is_system_admin = GroupRolesDAO::IsSystemAdmin($user_id) || UserRolesDAO::IsSystemAdmin($user_id); } return self::$is_system_admin; }
public static function IsSystemAdmin($user_id) { try { $ur = UserRolesDAO::getByPK($user_id, ADMIN_ROLE, 0); return !is_null($ur); } catch (Exception $e) { throw new InvalidDatabaseOperationException($e); } }