/** * Send a reset link to a given user. * * @param Request $request * @return RedirectResponse * @TODO: Authenticate the csrf, which must match, from the session. */ public function postEmail(Request $request) { $error = null; $message = null; $account = null; $email = $request->get('email'); $ninja_name = $request->get('ninja_name'); if (!$email && !$ninja_name) { $error = 'You must specify either an email or a ninja name!'; } else { if ($email) { $account = AccountFactory::findByEmail($email); } if (!isset($account)) { $account = AccountFactory::findByNinjaName($ninja_name); } if ($account === null || !$account->id()) { $error = 'Sorry, unable to find a matching account!'; } else { // PWR created with default nonce $request = PasswordResetRequest::generate($account); if ($this->sendEmail($request->nonce, $account)) { $message = 'Your reset email was sent!'; } else { $error = 'Sorry, there was a problem sending to your account! Please contact support.'; } } } return new RedirectResponse('/resetpassword.php?' . ($message ? 'message=' . url($message) . '&' : '') . ($error ? 'error=' . url($error) : '')); }
public function testPostEmailCanGetAnAccountUsingANinjaName() { $req = Request::create('/resetpassword.php'); $req->setMethod('POST'); $char = TestAccountCreateAndDestroy::char(); $ninja_name = $char->name(); $req->query->set('ninja_name', $ninja_name); $account = AccountFactory::findByNinjaName($ninja_name); $controller = new PasswordController(); $controller->postEmail($req); // Check for a matching request for the appropriate account. $req = PasswordResetRequest::where('_account_id', '=', $account->id())->first(); $this->assertNotEmpty($req, 'Fail: Unable to find a matching password reset request.'); }
/** * Display the main admin area * * Includes player viewing, account duplicates checking, npc balacing * * @return ViewSpec|RedirectResponse */ public function index() { $result = $this->requireAdmin($this->self); if ($result instanceof RedirectResponse) { return $result; } $viewChar = null; // View a target non-self character $charName = in('char_name'); if (is_string($charName) && trim($charName)) { $viewChar = get_char_id($charName); } // If a request is made to view a character's info, show it. $viewChar = first_value($viewChar, in('view')); $dupes = AdminViews::duped_ips(); $stats = AdminViews::high_rollers(); $npcs = NpcFactory::allNonTrivialNpcs(); $trivialNpcs = NpcFactory::allTrivialNpcs(); $charInfos = null; $charInventory = null; $firstMessage = null; $firstChar = null; $firstAccount = null; $firstDescription = null; if ($viewChar) { $ids = explode(',', $viewChar); $firstChar = new Player(reset($ids)); $firstAccount = AccountFactory::findByChar($firstChar); $charInfos = AdminViews::split_char_infos($viewChar); $charInventory = AdminViews::char_inventory($viewChar); $firstMessage = $firstChar->message(); $firstDescription = $firstChar->description(); } $parts = ['stats' => $stats, 'first_char' => $firstChar, 'first_description' => $firstDescription, 'first_message' => $firstMessage, 'first_account' => $firstAccount, 'char_infos' => $charInfos, 'dupes' => $dupes, 'char_inventory' => $charInventory, 'char_name' => $charName, 'npcs' => $npcs, 'trivial_npcs' => $trivialNpcs]; return ['title' => 'Admin Actions', 'template' => 'ninjamaster.tpl', 'parts' => $parts, 'options' => null]; }
/** * Leveling up Function * * @return boolean */ public function levelUp() { $health_to_add = 100; $turns_to_give = 50; $ki_to_give = 50; $stat_value_to_add = 5; $karma_to_give = 1; if ($this->isAdmin()) { // If the character is an admin, do not auto-level return false; } else { // For normal characters, do auto-level // Have to be under the max level and have enough kills. $level_up_possible = $this->level + 1 <= MAX_PLAYER_LEVEL && $this->kills >= $this->killsRequiredForNextLevel(); if ($level_up_possible) { // Perform the level up actions $this->set_health($this->health() + $health_to_add); $this->set_turns($this->turns() + $turns_to_give); $this->set_ki($this->ki() + $ki_to_give); // Must read from VO for these as accessors return modified values $this->setStamina($this->vo->stamina + $stat_value_to_add); $this->setStrength($this->vo->strength + $stat_value_to_add); $this->setSpeed($this->vo->speed + $stat_value_to_add); // no mutator for these yet $this->vo->kills = max(0, $this->kills - $this->killsRequiredForNextLevel()); $this->vo->karma = $this->karma + $karma_to_give; $this->vo->level = $this->level + 1; $this->save(); GameLog::recordLevelUp($this->id()); $account = AccountFactory::findByChar($this); $account->setKarmaTotal($account->getKarmaTotal() + $karma_to_give); AccountFactory::save($account); // Send a level-up message, for those times when auto-levelling happens. send_event($this->id(), $this->id(), "You levelled up! Your strength raised by {$stat_value_to_add}, speed by {$stat_value_to_add}, stamina by {$stat_value_to_add}, Karma by {$karma_to_give}, and your Ki raised {$ki_to_give}! You gained some health and turns, as well! You are now a level {$this->level} ninja! Go kill some stuff."); return true; } else { return false; } } }
/** * Get the account in a reliable manner. */ public function account() { assert($this->_account_id); return AccountFactory::findById($this->_account_id); }
public function testPerformingAResetInvalidatesUsedRequest() { $account_id = TestAccountCreateAndDestroy::account_id(); $account = AccountFactory::findById($account_id); PasswordResetRequest::generate($account, $this->nonce = '77warkwark', false); PasswordResetRequest::reset($account, 'new_pass34532'); $req = PasswordResetRequest::match($this->nonce); $this->assertEmpty($req); // Request shouldn't match because it should already be used. }
public function testAccountPasswordCanBeChanged() { $account = AccountFactory::make($this->testAccountId); $updated = $account->changePassword('whatever gibberish'); $this->assertTrue((bool) $updated); }