the browser-facing portion of osu!
at master 71 lines 1.6 kB view raw
1<?php 2 3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0. 4// See the LICENCE file in the repository root for full licence text. 5 6namespace App\Libraries; 7 8use App\Exceptions\AuthorizationException; 9use App\Exceptions\VerificationRequiredException; 10use Illuminate\Auth\AuthenticationException; 11 12class AuthorizationResult 13{ 14 private $rawMessage; 15 16 public function __construct($rawMessage) 17 { 18 $this->rawMessage = $rawMessage; 19 } 20 21 public function can() 22 { 23 return $this->rawMessage === 'ok'; 24 } 25 26 public function rawMessage() 27 { 28 if ($this->can()) { 29 return; 30 } 31 32 return presence($this->rawMessage, 'unauthorized'); 33 } 34 35 public function requireLogin() 36 { 37 return $this->rawMessage() === 'require_login' || 38 ends_with($this->rawMessage(), '.require_login'); 39 } 40 41 public function requireVerification() 42 { 43 return $this->rawMessage() === 'require_verification'; 44 } 45 46 public function message() 47 { 48 if ($this->can()) { 49 return; 50 } 51 52 return osu_trans('authorization.'.$this->rawMessage()); 53 } 54 55 public function ensureCan() 56 { 57 if ($this->can()) { 58 return; 59 } 60 61 if ($this->requireLogin()) { 62 $class = AuthenticationException::class; 63 } elseif ($this->requireVerification()) { 64 $class = VerificationRequiredException::class; 65 } else { 66 $class = AuthorizationException::class; 67 } 68 69 throw new $class($this->message()); 70 } 71}