diff options
Diffstat (limited to 'src')
38 files changed, 1143 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 diff --git a/src/config/.gitkeep b/src/config/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/config/.gitkeep diff --git a/src/config/botleaguesapi.php b/src/config/botleaguesapi.php new file mode 100644 index 0000000..1df9c5a --- /dev/null +++ b/src/config/botleaguesapi.php @@ -0,0 +1,15 @@ +<?php + +return [ + + 'default_format' => 'json', + + 'formats' => [ + + 'json' => 'Dingo\Api\Http\ResponseFormat\JsonpResponseFormat', + + ], + + 'allowed_origin' => ['http://botleagues.camilstaps.nl', 'https://botleagues.camilstaps.nl'] + +]; diff --git a/src/controllers/.gitkeep b/src/controllers/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/controllers/.gitkeep diff --git a/src/controllers/BaseController.php b/src/controllers/BaseController.php new file mode 100644 index 0000000..78bda5f --- /dev/null +++ b/src/controllers/BaseController.php @@ -0,0 +1,10 @@ +<?php +namespace CamilStaps\BotleaguesApi\Controllers; + +use \Dingo\Api\Routing\Helpers; + +class BaseController extends \Illuminate\Routing\Controller { + + use Helpers; + +}
\ No newline at end of file diff --git a/src/controllers/BotController.php b/src/controllers/BotController.php new file mode 100644 index 0000000..89aef8c --- /dev/null +++ b/src/controllers/BotController.php @@ -0,0 +1,30 @@ +<?php +namespace CamilStaps\BotleaguesApi\Controllers; + +use CamilStaps\BotleaguesApi\Database\Bot; + +class BotController extends BaseController { + + protected $bot; + + public function __construct(Bot $bot) { + $this->bot = $bot; + } + + public function index() { + return $this->bot->all(); + } + + public function show($id) { + return $this->bot->findOrFail($id); + } + + public function update($id) { + throw new \Exception("Not implemented yet."); + } + + public function store() { + throw new \Exception("Not implemented yet."); + } + +}
\ No newline at end of file diff --git a/src/controllers/CompetitionController.php b/src/controllers/CompetitionController.php new file mode 100644 index 0000000..f54c11d --- /dev/null +++ b/src/controllers/CompetitionController.php @@ -0,0 +1,37 @@ +<?php +namespace CamilStaps\BotleaguesApi\Controllers; + +use CamilStaps\BotleaguesApi\Database\Competition; +use Illuminate\Support\Facades\Input; + +class CompetitionController extends BaseController { + + protected $competition; + + public function __construct(Competition $competition) { + $this->competition = $competition; + } + + public function index() { + return $this->competition->all(); + } + + public function show($id) { + return $this->competition->findOrFail($id); + } + + public function update($id) { + throw new \Exception("Not implemented yet."); + } + + public function store() { + $this->competition->fill(Input::all()); + + if ($this->competition->save()) { + return $this->response->created(); + } else { + throw new \Dingo\Api\Exception\StoreResourceFailedException; + } + } + +}
\ No newline at end of file diff --git a/src/controllers/CompetitionTypeController.php b/src/controllers/CompetitionTypeController.php new file mode 100644 index 0000000..993973c --- /dev/null +++ b/src/controllers/CompetitionTypeController.php @@ -0,0 +1,37 @@ +<?php +namespace CamilStaps\BotleaguesApi\Controllers; + +use CamilStaps\BotleaguesApi\Database\CompetitionType; +use Illuminate\Support\Facades\Input; + +class CompetitionTypeController extends BaseController { + + protected $competitionType; + + public function __construct(CompetitionType $competitionType) { + $this->competitionType = $competitionType; + } + + public function index() { + return $this->competitionType->all(); + } + + public function show($id) { + return $this->competitionType->findOrFail($id); + } + + public function update($id) { + throw new \Exception("Not implemented yet."); + } + + public function store() { + $this->competitionType->fill(Input::all()); + + if ($this->competitionType->save()) { + return $this->response->created(); + } else { + throw new \Dingo\Api\Exception\StoreResourceFailedException; + } + } + +}
\ No newline at end of file diff --git a/src/controllers/GameController.php b/src/controllers/GameController.php new file mode 100644 index 0000000..0fbcc1b --- /dev/null +++ b/src/controllers/GameController.php @@ -0,0 +1,37 @@ +<?php +namespace CamilStaps\BotleaguesApi\Controllers; + +use CamilStaps\BotleaguesApi\Database\Game; +use Illuminate\Support\Facades\Input; + +class GameController extends BaseController { + + protected $game; + + public function __construct(Game $game) { + $this->game = $game; + } + + public function index() { + return $this->game->all(); + } + + public function show($id) { + return $this->game->findOrFail($id); + } + + public function update($id) { + throw new \Exception("Not implemented yet."); + } + + public function store() { + $this->game->fill(Input::all()); + + if ($this->game->save()) { + return $this->response->created(); + } else { + throw new \Dingo\Api\Exception\StoreResourceFailedException; + } + } + +}
\ No newline at end of file diff --git a/src/controllers/ParticipantController.php b/src/controllers/ParticipantController.php new file mode 100644 index 0000000..11d7dd9 --- /dev/null +++ b/src/controllers/ParticipantController.php @@ -0,0 +1,37 @@ +<?php +namespace CamilStaps\BotleaguesApi\Controllers; + +use CamilStaps\BotleaguesApi\Database\Participant; +use Illuminate\Support\Facades\Input; + +class ParticipantController extends BaseController { + + protected $participant; + + public function __construct(Participant $participant) { + $this->participant = $participant; + } + + public function index() { + return $this->participant->all(); + } + + public function show($id) { + return $this->participant->findOrFail($id); + } + + public function update($id) { + throw new \Exception("Not implemented yet."); + } + + public function store() { + $this->participant->fill(Input::all()); + + if ($this->participant->save()) { + return $this->response->created(); + } else { + throw new \Dingo\Api\Exception\StoreResourceFailedException; + } + } + +}
\ No newline at end of file diff --git a/src/controllers/PasswordReminderController.php b/src/controllers/PasswordReminderController.php new file mode 100644 index 0000000..bf9d959 --- /dev/null +++ b/src/controllers/PasswordReminderController.php @@ -0,0 +1,47 @@ +<?php +/** + * Created by PhpStorm. + * User: camilstaps + * Date: 13-5-15 + * Time: 13:13 + */ + +namespace CamilStaps\BotleaguesApi\Controllers; + +use CamilStaps\BotleaguesApi\Database\PasswordReminder; +use CamilStaps\BotleaguesApi\Exception\ValidationException; +use Illuminate\Support\Facades\Input; +use Illuminate\Support\Facades\Validator; + +class PasswordReminderController extends BaseController { + + protected $passwordReminder; + + public function __construct(PasswordReminder $passwordReminder) { + $this->passwordReminder = $passwordReminder; + } + + /** + * Set the userId and create a random token + */ + public function store() { + $rules = [ + 'user_id' => ['required'] + ]; + $payload = Input::only('user_id'); + $validator = Validator::make($payload, $rules); + if ($validator->fails()) { + throw new ValidationException('Could not find user.', $validator->errors()); + } + + $this->passwordReminder->userId = Input::get('user_id'); + $this->passwordReminder->token = base64_encode(openssl_random_pseudo_bytes(64)); + + if ($this->passwordReminder->save()) { + return $this->passwordReminder; + } else { + throw new \Dingo\Api\Exception\StoreResourceFailedException; + } + } + +}
\ No newline at end of file diff --git a/src/controllers/UserController.php b/src/controllers/UserController.php new file mode 100644 index 0000000..6af3fe8 --- /dev/null +++ b/src/controllers/UserController.php @@ -0,0 +1,63 @@ +<?php +namespace CamilStaps\BotleaguesApi\Controllers; + +use CamilStaps\BotleaguesApi\Database\User; +use CamilStaps\BotleaguesApi\Exception\ValidationException; +use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Hash; +use Illuminate\Support\Facades\Input; +use Illuminate\Support\Facades\Validator; + +class UserController extends BaseController { + + protected $user; + + public function __construct(User $user) { + $this->user = $user; + + $this->beforeFilter('current_user', array('only' => ['update', 'destroy'])); + } + + public function index() { + return $this->user->all(); + } + + public function show($id) { + return $this->user->findOrFail($id); + } + + public function update($id) { + $s = $this->user->find($id); + + if (!empty(Input::get('password'))) + $s->password = Hash::make(Input::get('password')); + + if ($s->save()) { + return $this->show($id); + } else { + throw new \Dingo\Api\Exception\UpdateResourceFailedException; + } + } + + public function store() { + $rules = [ + 'email' => ['required', 'email', 'unique:users'], + 'password' => ['required', 'min:7'] + ]; + $payload = Input::only('email', 'password'); + $validator = Validator::make($payload, $rules); + if ($validator->fails()) { + throw new ValidationException('Could not create new user.', $validator->errors()); + } + + $this->user->email = Input::get('email'); + $this->user->password = empty(Input::get('password')) ? null : Hash::make(Input::get('password')); + + if ($this->user->save()) { + return $this->response->created(); + } else { + throw new \Dingo\Api\Exception\StoreResourceFailedException; + } + } + +}
\ No newline at end of file diff --git a/src/controllers/UserTokenController.php b/src/controllers/UserTokenController.php new file mode 100644 index 0000000..20bd06c --- /dev/null +++ b/src/controllers/UserTokenController.php @@ -0,0 +1,51 @@ +<?php +namespace CamilStaps\BotleaguesApi\Controllers; + +use CamilStaps\BotleaguesApi\Database\UserToken; +use Illuminate\Support\Facades\Input; +use Illuminate\Support\Facades\Auth; + +class UserTokenController extends BaseController { + + protected $userToken; + + public function __construct(UserToken $userToken) { + $this->userToken = $userToken; + } + + /** + * Only the tokens of the authenticated user are shown + */ + public function index() { + return $this->userToken->where('userId', '=', Auth::user()->id)->get(); + } + + /** + * Only the tokens of the authenticated user are available + */ + public function show($id) { + return $this->userToken->where('userId', '=', Auth::user()->id)->findOrFail($id); + } + + /** + * Set the userId and create a random token + */ + public function store() { + $this->userToken->userId = Auth::user()->id; + $this->userToken->token = base64_encode(openssl_random_pseudo_bytes(64)); + + if ($this->userToken->save()) { + // Remove the token field from the hidden fields + $hidden = $this->userToken->getHidden(); + foreach ($hidden as $k => $v) + if ($v == 'token') + unset($hidden[$k]); + $this->userToken->setHidden($hidden); + + return $this->userToken; + } else { + throw new \Dingo\Api\Exception\StoreResourceFailedException; + } + } + +}
\ No newline at end of file diff --git a/src/filters.php b/src/filters.php new file mode 100644 index 0000000..11f0096 --- /dev/null +++ b/src/filters.php @@ -0,0 +1,16 @@ +<?php +Route::filter('administrator', function(){ + Auth::basic(); + + if (!Auth::user()->isAdministrator) { + throw new \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException(); + } +}); + +Route::filter('current_user', function(){ + Auth::basic(); + + if (empty(Auth::user()) || Route::input('user') != Auth::user()->id) { + throw new \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException(); + } +});
\ No newline at end of file diff --git a/src/lang/.gitkeep b/src/lang/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/lang/.gitkeep diff --git a/src/migrations/.gitkeep b/src/migrations/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/migrations/.gitkeep diff --git a/src/migrations/2015_02_23_184402_botleaguesapi-create_users.php b/src/migrations/2015_02_23_184402_botleaguesapi-create_users.php new file mode 100644 index 0000000..b054a43 --- /dev/null +++ b/src/migrations/2015_02_23_184402_botleaguesapi-create_users.php @@ -0,0 +1,36 @@ +<?php + +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Migrations\Migration; + +class BotleaguesapiCreateUsers extends Migration { + + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::create('users', function(Blueprint $table) + { + $table->increments('id')->unsigned(); + $table->string('email', 127)->unique(); + $table->string('password', 60); + $table->rememberToken(); + $table->boolean('isAdministrator')->default(false); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('users'); + } + +} diff --git a/src/migrations/2015_02_23_184407_botleaguesapi-create_games.php b/src/migrations/2015_02_23_184407_botleaguesapi-create_games.php new file mode 100644 index 0000000..3ecc1e2 --- /dev/null +++ b/src/migrations/2015_02_23_184407_botleaguesapi-create_games.php @@ -0,0 +1,33 @@ +<?php + +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Migrations\Migration; + +class BotleaguesapiCreateGames extends Migration { + + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::create('games', function(Blueprint $table) + { + $table->increments('id')->unsigned(); + $table->string('title')->unique(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('games'); + } + +} diff --git a/src/migrations/2015_02_23_184413_botleaguesapi-create_bots.php b/src/migrations/2015_02_23_184413_botleaguesapi-create_bots.php new file mode 100644 index 0000000..11ab786 --- /dev/null +++ b/src/migrations/2015_02_23_184413_botleaguesapi-create_bots.php @@ -0,0 +1,39 @@ +<?php + +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Migrations\Migration; + +class BotleaguesapiCreateBots extends Migration { + + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::create('bots', function(Blueprint $table) + { + $table->increments('id')->unsigned(); + $table->integer('userId')->unsigned(); + $table->foreign('userId')->references('id')->on('users'); + $table->integer('gameId')->unsigned(); + $table->foreign('gameId')->references('id')->on('games'); + $table->string('title', 45); + $table->string('version', 12); + $table->unique(array('gameId', 'title', 'version')); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('bots'); + } + +} diff --git a/src/migrations/2015_04_26_192015_botleaguesapi-create_competition_types.php b/src/migrations/2015_04_26_192015_botleaguesapi-create_competition_types.php new file mode 100644 index 0000000..280bb6d --- /dev/null +++ b/src/migrations/2015_04_26_192015_botleaguesapi-create_competition_types.php @@ -0,0 +1,34 @@ +<?php + +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Migrations\Migration; + +class BotleaguesapiCreateCompetitionTypes extends Migration { + + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::create('competition_types', function(Blueprint $table) + { + $table->increments('type'); + $table->string('description'); + $table->mediumText('explanation'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('competition_types'); + } + +} diff --git a/src/migrations/2015_04_26_192025_botleaguesapi-create_competitions.php b/src/migrations/2015_04_26_192025_botleaguesapi-create_competitions.php new file mode 100644 index 0000000..8652086 --- /dev/null +++ b/src/migrations/2015_04_26_192025_botleaguesapi-create_competitions.php @@ -0,0 +1,41 @@ +<?php + +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Migrations\Migration; + +class BotleaguesapiCreateCompetitions extends Migration { + + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::create('competitions', function(Blueprint $table) + { + $table->increments('id')->unsigned(); + $table->integer('gameId')->unsigned(); + $table->foreign('gameId')->references('id')->on('games'); + $table->string('title', 127)->unique(); + $table->mediumText('description')->nullable(); + $table->integer('competition_type')->unsigned(); + $table->foreign('competition_type')->references('type')->on('competition_types'); + $table->dateTime('start'); + $table->dateTime('end'); + $table->mediumInteger('max_players')->unsigned(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('competitions'); + } + +} diff --git a/src/migrations/2015_04_26_192035_botleaguesapi-create_participants.php b/src/migrations/2015_04_26_192035_botleaguesapi-create_participants.php new file mode 100644 index 0000000..9b76316 --- /dev/null +++ b/src/migrations/2015_04_26_192035_botleaguesapi-create_participants.php @@ -0,0 +1,36 @@ +<?php + +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Migrations\Migration; + +class BotleaguesapiCreateParticipants extends Migration { + + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::create('participants', function(Blueprint $table) + { + $table->increments('id'); + $table->integer('botId')->unsigned(); + $table->foreign('botId')->references('id')->on('bots'); + $table->integer('competitionId')->unsigned(); + $table->foreign('competitionId')->references('id')->on('competitions'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('participants'); + } + +} diff --git a/src/migrations/2015_05_10_134654_botleaguesapi-create_user_tokens.php b/src/migrations/2015_05_10_134654_botleaguesapi-create_user_tokens.php new file mode 100644 index 0000000..3d8c82e --- /dev/null +++ b/src/migrations/2015_05_10_134654_botleaguesapi-create_user_tokens.php @@ -0,0 +1,36 @@ +<?php + +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Migrations\Migration; + +class BotleaguesapiCreateUserTokens extends Migration { + + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::create('user_tokens', function(Blueprint $table) + { + $table->increments('id'); + $table->integer('userId')->unsigned(); + $table->foreign('userId')->references('id')->on('users'); + $table->string('token'); + $table->timestamp('valid_till'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('user_tokens'); + } + +} diff --git a/src/migrations/2015_05_13_105945_botleaguesapi-create_password_reminders.php b/src/migrations/2015_05_13_105945_botleaguesapi-create_password_reminders.php new file mode 100644 index 0000000..a2473fb --- /dev/null +++ b/src/migrations/2015_05_13_105945_botleaguesapi-create_password_reminders.php @@ -0,0 +1,36 @@ +<?php + +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Migrations\Migration; + +class BotleaguesApiCreatePasswordReminders extends Migration { + + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::create('password_reminders', function(Blueprint $table) + { + $table->integer('userId')->unsigned(); + $table->foreign('userId')->references('id')->on('users'); + $table->string('token')->index(); + $table->timestamp('created_at'); + $table->timestamp('valid_till'); + $table->timestamp('used_at')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('password_reminders'); + } + +} diff --git a/src/routes.php b/src/routes.php new file mode 100644 index 0000000..def14d1 --- /dev/null +++ b/src/routes.php @@ -0,0 +1,60 @@ +<?php +Route::group(array('https'), function() use ($api) { + + $api->version('v1', ['protected' => false], function ($api) { + + $api->resource('bot', 'CamilStaps\BotleaguesApi\Controllers\BotController', + ['only' => ['index','show']]); + + $api->resource('competition', 'CamilStaps\BotleaguesApi\Controllers\CompetitionController', + ['only' => ['index','show']]); + + $api->resource('competition_type', 'CamilStaps\BotleaguesApi\Controllers\CompetitionTypeController', + ['only' => ['index','show']]); + + $api->resource('game', 'CamilStaps\BotleaguesApi\Controllers\GameController', + ['only' => ['index','show']]); + + $api->resource('participant', 'CamilStaps\BotleaguesApi\Controllers\ParticipantController', + ['only' => ['index','show']]); + + $api->resource('user', 'CamilStaps\BotleaguesApi\Controllers\UserController', + ['only' => ['index','show','store']]); + + $api->resource('password_reminder', 'CamilStaps\BotleaguesApi\Controllers\PasswordReminderController', + ['only' => ['store']]); + + }); + + $api->version('v1', ['protected' => true, 'providers' => 'basic'], function ($api) { + $api->resource('user_token', 'CamilStaps\BotleaguesApi\Controllers\UserTokenController', + ['only' => ['store']]); + }); + + $api->version('v1', ['protected' => true, 'providers' => 'token'], function ($api) { + + $api->resource('bot', 'CamilStaps\BotleaguesApi\Controllers\BotController', + ['except' => ['index', 'show', 'create','edit']]); + + $api->resource('user', 'CamilStaps\BotleaguesApi\Controllers\UserController', + ['except' => ['index', 'show', 'create','edit','store']]); + + $api->resource('user_token', 'CamilStaps\BotleaguesApi\Controllers\UserTokenController', + ['only' => ['index', 'show']]); + + $api->group(array('before' => 'administrator'), function() use ($api) { + + $api->resource('competition', 'CamilStaps\BotleaguesApi\Controllers\CompetitionController', + ['except' => ['index', 'show', 'create', 'edit']]); + + $api->resource('competition_type', 'CamilStaps\BotleaguesApi\Controllers\CompetitionTypeController', + ['except' => ['index', 'show', 'create', 'edit']]); + + $api->resource('game', 'CamilStaps\BotleaguesApi\Controllers\GameController', + ['except' => ['index', 'show', 'create','edit']]); + + }); + + }); + +});
\ No newline at end of file diff --git a/src/views/.gitkeep b/src/views/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/views/.gitkeep diff --git a/src/views/emails/auth/reminder.blade.php b/src/views/emails/auth/reminder.blade.php new file mode 100644 index 0000000..bd8ad22 --- /dev/null +++ b/src/views/emails/auth/reminder.blade.php @@ -0,0 +1,14 @@ +<!DOCTYPE html> +<html lang="en-US"> +<head> + <meta charset="utf-8"> +</head> +<body> +<h2>Password Reset</h2> + +<div> + To reset your password, use the following token:<br/> + {{{ $token }}} +</div> +</body> +</html> |