diff options
Diffstat (limited to 'src/CamilStaps')
12 files changed, 398 insertions, 0 deletions
diff --git a/src/CamilStaps/BotleaguesApi/BotleaguesApiServiceProvider.php b/src/CamilStaps/BotleaguesApi/BotleaguesApiServiceProvider.php new file mode 100644 index 0000000..bb90671 --- /dev/null +++ b/src/CamilStaps/BotleaguesApi/BotleaguesApiServiceProvider.php @@ -0,0 +1,111 @@ +<?php +namespace CamilStaps\BotleaguesApi; + +use \Illuminate\Support\Facades\Request; +use \Illuminate\Support\ServiceProvider; +use Response; + +class BotleaguesApiServiceProvider extends ServiceProvider { + + /** + * Indicates if loading of the provider is deferred. + * + * @var bool + */ + protected $defer = false; + + /** + * Bootstrap the application events. + * + * @return void + */ + public function boot() + { + #$this->package('camil-staps/botleagues-api', null, __DIR__.'/../..'); + #$this->loadAutoloader(base_path('vendor')); + + $this->loadViewsFrom(__DIR__ . '/../../views', 'botleagues-api'); + $this->publishes([ __DIR__ . '/../../views' => base_path('resources/view/vendor/botleagues-api')], 'views'); + + $this->publishes([ __DIR__ . '/../../config/botleaguesapi.php' => config_path('botleaguesapi.php')], 'config'); + + include __DIR__ . '/../../filters.php'; + + $api = app('api.router'); + include __DIR__ . '/../../routes.php'; + + // To allow loading API requests from the specified domain + $allowed_origin = config('botleaguesapi.allowed_origin'); + if (is_array($allowed_origin)) { + $origin = Request::header('Origin'); + if (in_array($origin, $allowed_origin)) { + header('Access-Control-Allow-Origin: ' . $origin); + } else { + header('Access-Control-Allow-Origin: ' . $allowed_origin[0]); + } + } else { + header('Access-Control-Allow-Origin: ' . $allowed_origin); + } + header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE'); + header('Access-Control-Allow-Headers: Authorization'); + + $this->setupErrorHandlers(); + } + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return array('CamilStaps\BotleaguesApi\BotleaguesApiServiceProvider'); + } + + /** + * Setup nice error handlers for exceptions and fatal errors + */ + private function setupErrorHandlers() { + $exception = app('api.exception'); + + $exception->register(function(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e) { + return Response::make( + ['error' => 'Endpoint not found'], + 404); + }); + $exception->register(function(\Illuminate\Database\Eloquent\ModelNotFoundException $e) { + return Response::make( + ['error' => 'Resource not found'], + 404); + }); + $exception->register(function(\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException $e) { + return Response::make( + ['error' => !config('app.debug') || empty($e->getMessage()) ? 'Access denied' : $e->getMessage()], + 404); + }); + $exception->register(function(Exception\ValidationException $e) { + return Response::make( + [ + 'error' => $e->getMessage(), + 'errors' => $e->errors + ], + 500); + }); + + $exception->register(function(\Exception $e) { + return Response::make( + ['error' => config('app.debug') ? $e->getMessage() : "Internal error"], + 500); + }); + } + +} diff --git a/src/CamilStaps/BotleaguesApi/Database/Bot.php b/src/CamilStaps/BotleaguesApi/Database/Bot.php new file mode 100644 index 0000000..f0be0fc --- /dev/null +++ b/src/CamilStaps/BotleaguesApi/Database/Bot.php @@ -0,0 +1,9 @@ +<?php +namespace CamilStaps\BotleaguesApi\Database; + +class Bot extends Model { + + protected $table = 'bots'; + protected $fillable = ['userId', 'gameId', 'title']; + +}
\ No newline at end of file diff --git a/src/CamilStaps/BotleaguesApi/Database/Competition.php b/src/CamilStaps/BotleaguesApi/Database/Competition.php new file mode 100644 index 0000000..c355c08 --- /dev/null +++ b/src/CamilStaps/BotleaguesApi/Database/Competition.php @@ -0,0 +1,10 @@ +<?php +namespace CamilStaps\BotleaguesApi\Database; + +class Competition extends Model { + + protected $table = 'competitions'; + protected $hidden = []; + protected $fillable = ['gameId', 'title', 'description', 'competition_type', 'start', 'end', 'max_players']; + +}
\ No newline at end of file diff --git a/src/CamilStaps/BotleaguesApi/Database/CompetitionType.php b/src/CamilStaps/BotleaguesApi/Database/CompetitionType.php new file mode 100644 index 0000000..0107ffe --- /dev/null +++ b/src/CamilStaps/BotleaguesApi/Database/CompetitionType.php @@ -0,0 +1,11 @@ +<?php +namespace CamilStaps\BotleaguesApi\Database; + +class CompetitionType extends Model { + + protected $table = 'competition_types'; + protected $primaryKey = 'type'; + protected $hidden = []; + protected $fillable = ['description', 'explanation']; + +}
\ No newline at end of file diff --git a/src/CamilStaps/BotleaguesApi/Database/Game.php b/src/CamilStaps/BotleaguesApi/Database/Game.php new file mode 100644 index 0000000..a8b0649 --- /dev/null +++ b/src/CamilStaps/BotleaguesApi/Database/Game.php @@ -0,0 +1,9 @@ +<?php +namespace CamilStaps\BotleaguesApi\Database; + +class Game extends Model { + + protected $table = 'games'; + protected $fillable = ['title']; + +}
\ No newline at end of file diff --git a/src/CamilStaps/BotleaguesApi/Database/Model.php b/src/CamilStaps/BotleaguesApi/Database/Model.php new file mode 100644 index 0000000..27bd1fc --- /dev/null +++ b/src/CamilStaps/BotleaguesApi/Database/Model.php @@ -0,0 +1,30 @@ +<?php +/** + * Created by PhpStorm. + * User: camilstaps + * Date: 14-5-15 + * Time: 10:47 + */ + +namespace CamilStaps\BotleaguesApi\Database; + +use Carbon\Carbon; + + +class Model extends \Illuminate\Database\Eloquent\Model { + + protected $date_format = Carbon::RFC2822; + + protected function formatDate($date) { + return Carbon::parse($date)->format($this->date_format); + } + + public function getCreatedAtAttribute($attr) { + return $this->formatDate($attr); + } + + public function getUpdatedAtAttribute($attr) { + return $this->formatDate($attr); + } + +}
\ No newline at end of file diff --git a/src/CamilStaps/BotleaguesApi/Database/Participant.php b/src/CamilStaps/BotleaguesApi/Database/Participant.php new file mode 100644 index 0000000..584d00c --- /dev/null +++ b/src/CamilStaps/BotleaguesApi/Database/Participant.php @@ -0,0 +1,10 @@ +<?php +namespace CamilStaps\BotleaguesApi\Database; + +class Participant extends Model { + + protected $table = 'competition_types'; + protected $hidden = []; + protected $fillable = ['botId', 'competitionId']; + +}
\ No newline at end of file diff --git a/src/CamilStaps/BotleaguesApi/Database/PasswordReminder.php b/src/CamilStaps/BotleaguesApi/Database/PasswordReminder.php new file mode 100644 index 0000000..65c4773 --- /dev/null +++ b/src/CamilStaps/BotleaguesApi/Database/PasswordReminder.php @@ -0,0 +1,38 @@ +<?php +/** + * Created by PhpStorm. + * User: camilstaps + * Date: 13-5-15 + * Time: 13:12 + */ + +namespace CamilStaps\BotleaguesApi\Database; + +use Illuminate\Support\Facades\Mail; + +class PasswordReminder extends Model { + + protected $table = 'password_reminders'; + protected $hidden = ['token']; + protected $fillable = ['userId', 'token', 'valid_till']; + + /** + * Override the parent's save() function to automatically update the valid_till timestamp, and send an email + */ + public function save(array $options = array()) { + $this->valid_till = date("Y-m-d H:i:s", time() + 3600); + + $user = User::findOrFail($this->userId); + Mail::send('botleagues-api::emails.auth.reminder', ['token' => $this->token], function($message) use ($user) { + $message->to($user->email, "User " . $user->id); + }); + + return parent::save($options); + } + + /** + * Disable updated_at timestamp + */ + public function setUpdatedAtAttribute($value) {} + +}
\ No newline at end of file diff --git a/src/CamilStaps/BotleaguesApi/Database/User.php b/src/CamilStaps/BotleaguesApi/Database/User.php new file mode 100644 index 0000000..0af91c3 --- /dev/null +++ b/src/CamilStaps/BotleaguesApi/Database/User.php @@ -0,0 +1,79 @@ +<?php +namespace CamilStaps\BotleaguesApi\Database; + +use Illuminate\Auth\Authenticatable; +use Illuminate\Auth\Passwords\CanResetPassword; +use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; +use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; + +class User extends Model implements AuthenticatableContract, CanResetPasswordContract { + + use Authenticatable, CanResetPassword; + + protected $table = 'users'; + protected $hidden = ['password', 'remember_token', 'api_key']; + protected $fillable = ['email', 'password']; + + public function isAdministrator() { + return (bool) $this->isAdministrator; + } + + public function validToken($token) { + return UserToken::where('userId', $this->id)->where('token', $token)->where('valid_till', '>', date("Y-m-d H:i:s"))->count() > 0; + } + + /** + * Get the unique identifier for the user. + * + * @return mixed + */ + public function getAuthIdentifier() { + return $this->getKey(); + } + + /** + * Get the password for the user. + * @todo not implemented yet + * @return string + */ + public function getAuthPassword() { + return $this->password; + } + + /** + * Get the token value for the "remember me" session. + * @todo not implemented yet + * @return string + */ + public function getRememberToken() { + return null; + } + + /** + * Set the token value for the "remember me" session. + * @todo not implemented yet + * @param string $value + * @return void + */ + public function setRememberToken($value) { + return null; + } + + /** + * Get the column name for the "remember me" token. + * @todo not implemented yet + * @return string + */ + public function getRememberTokenName() { + return null; + } + + /** + * Get the e-mail address where password reset links are sent. + * + * @return string + */ + public function getEmailForPasswordReset() { + return $this->email; + } +}
\ No newline at end of file diff --git a/src/CamilStaps/BotleaguesApi/Database/UserToken.php b/src/CamilStaps/BotleaguesApi/Database/UserToken.php new file mode 100644 index 0000000..92b03b8 --- /dev/null +++ b/src/CamilStaps/BotleaguesApi/Database/UserToken.php @@ -0,0 +1,24 @@ +<?php +namespace CamilStaps\BotleaguesApi\Database; + +class UserToken extends Model { + + protected $table = 'user_tokens'; + protected $hidden = ['token']; + protected $fillable = ['userId', 'token', 'valid_till']; + protected $dates = ['created_at', 'updated_at', 'valid_till']; + + /** + * Override the parent's save() function to automatically update the valid_till timestamp + */ + public function save(array $options = array()) { + $this->valid_till = date("Y-m-d H:i:s", time() + 3600); + + return parent::save($options); + } + + public function getValidTillAttribute($attr) { + return $this->formatDate($attr); + } + +}
\ No newline at end of file diff --git a/src/CamilStaps/BotleaguesApi/Exception/ValidationException.php b/src/CamilStaps/BotleaguesApi/Exception/ValidationException.php new file mode 100644 index 0000000..c59ae6f --- /dev/null +++ b/src/CamilStaps/BotleaguesApi/Exception/ValidationException.php @@ -0,0 +1,18 @@ +<?php +namespace CamilStaps\BotleaguesApi\Exception; + +use Symfony\Component\HttpKernel\Exception\HttpException; + +class ValidationException extends HttpException { + + public function __construct($message = null, $errors = null, Exception $previous = null, $headers = [], $code = 0) { + if (is_null($errors)) { + $this->errors = new MessageBag; + } else { + $this->errors = is_array($errors) ? new MessageBag($errors) : $errors; + } + + parent::__construct(422, $message, $previous, $headers, $code); + } + +}
\ No newline at end of file diff --git a/src/CamilStaps/BotleaguesApi/TokenAuthenticationProvider.php b/src/CamilStaps/BotleaguesApi/TokenAuthenticationProvider.php new file mode 100644 index 0000000..3d15262 --- /dev/null +++ b/src/CamilStaps/BotleaguesApi/TokenAuthenticationProvider.php @@ -0,0 +1,49 @@ +<?php +/** + * Created by PhpStorm. + * User: camilstaps + * Date: 12-5-15 + * Time: 14:41 + */ + +namespace CamilStaps\BotleaguesApi; + +use Dingo\Api\Auth\Provider\Provider; +use Dingo\Api\Routing\Route; +use Illuminate\Http\Request; +use Illuminate\Support\Facades\Auth; +use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; + +class TokenAuthenticationProvider implements Provider { + + /** + * Authenticate the request and return the authenticated user instance. + * + * @param \Illuminate\Http\Request $request + * @param \Dingo\Api\Routing\Route $route + * + * @return mixed + */ + public function authenticate(Request $request, Route $route) { + if (!$request->has(['user_id', 'token'])) { + throw new UnauthorizedHttpException(null, "Include user_id and token in your request."); + } + + $user = User::find($request->get('user_id')); + if ($user != null && $user->validToken($request->get('token'))) { + Auth::login($user); + return Auth::user(); + } + + throw new UnauthorizedHttpException(null, "Invalid credentials"); + } + + /** + * Get the providers authorization method. + * + * @return string + */ + public function getAuthorizationMethod() { + return 'token'; + } +}
\ No newline at end of file |