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; }
/** * 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; }
/** * Adds an admin to a contest * * @param Request $r * @return array * @throws InvalidDatabaseOperationException * @throws ForbiddenAccessException */ public static function apiAddAdmin(Request $r) { if (OMEGAUP_LOCKDOWN) { throw new ForbiddenAccessException("lockdown"); } // Authenticate logged user self::authenticateRequest($r); // Check contest_alias Validators::isStringNonEmpty($r["contest_alias"], "contest_alias"); $user = UserController::resolveUser($r["usernameOrEmail"]); try { $r["contest"] = ContestsDAO::getByAlias($r["contest_alias"]); } catch (Exception $e) { // Operation failed in the data layer throw new InvalidDatabaseOperationException($e); } // Only director is allowed to create problems in contest if (!Authorization::IsContestAdmin($r["current_user_id"], $r["contest"])) { throw new ForbiddenAccessException(); } $contest_user = new UserRoles(); $contest_user->setContestId($r["contest"]->getContestId()); $contest_user->setUserId($user->getUserId()); $contest_user->setRoleId(CONTEST_ADMIN_ROLE); // Save the contest to the DB try { UserRolesDAO::save($contest_user); } catch (Exception $e) { // Operation failed in the data layer throw new InvalidDatabaseOperationException($e); } return array("status" => "ok"); }
/** * Adds an admin to a problem * * @param Request $r * @return array * @throws InvalidDatabaseOperationException * @throws ForbiddenAccessException */ public static function apiAddAdmin(Request $r) { // Authenticate logged user self::authenticateRequest($r); // Check problem_alias Validators::isStringNonEmpty($r['problem_alias'], 'problem_alias'); $user = UserController::resolveUser($r['usernameOrEmail']); try { $r['problem'] = ProblemsDAO::getByAlias($r['problem_alias']); } catch (Exception $e) { // Operation failed in the data layer throw new InvalidDatabaseOperationException($e); } if (!Authorization::IsProblemAdmin($r['current_user_id'], $r['problem'])) { throw new ForbiddenAccessException(); } $user_role = new UserRoles(); $user_role->setContestId($r['problem']->problem_id); $user_role->setUserId($user->user_id); $user_role->setRoleId(PROBLEM_ADMIN_ROLE); // Save the contest to the DB try { UserRolesDAO::save($user_role); } catch (Exception $e) { // Operation failed in the data layer self::$log->error('Failed to save user roles'); self::$log->error($e); throw new InvalidDatabaseOperationException($e); } return array('status' => 'ok'); }