diff options
19 files changed, 401 insertions, 16 deletions
diff --git a/src/CamilStaps/BotleaguesApi/Bot.php b/src/CamilStaps/BotleaguesApi/Bot.php index f45ada7..04fa2c7 100644 --- a/src/CamilStaps/BotleaguesApi/Bot.php +++ b/src/CamilStaps/BotleaguesApi/Bot.php @@ -5,6 +5,7 @@ use Illuminate\Database\Eloquent\Model; class Bot extends Model { - + protected $table = 'bots'; + protected $fillable = ['userId', 'gameId', 'title']; }
\ No newline at end of file diff --git a/src/CamilStaps/BotleaguesApi/BotleaguesApiServiceProvider.php b/src/CamilStaps/BotleaguesApi/BotleaguesApiServiceProvider.php index 012b5cf..22ec44c 100644 --- a/src/CamilStaps/BotleaguesApi/BotleaguesApiServiceProvider.php +++ b/src/CamilStaps/BotleaguesApi/BotleaguesApiServiceProvider.php @@ -4,6 +4,7 @@ namespace CamilStaps\BotleaguesApi; use \Illuminate\Database\Eloquent\ModelNotFoundException; use \Illuminate\Support\ServiceProvider; use \Illuminate\Support\Facades\App; +use \Illuminate\Support\Facades\Config; use \Dingo\Api\Facade\API; use Response; @@ -27,15 +28,27 @@ class BotleaguesApiServiceProvider extends ServiceProvider { include __DIR__ . '/../../filters.php'; include __DIR__ . '/../../routes.php'; + + App::fatal(function($e) { + return Response::make( + ['error' => Config::get('app.debug') ? $e->getMessage() : "Internal error"], + 500); + }); API::error(function(ModelNotFoundException $e) { - return Response::make(['error' => 'Resource not found'], 404); + return Response::make( + ['error' => 'Resource not found'], + 404); }); API::error(function(\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException $e) { - return Response::make(['error' => $e->getMessage() == null ? 'Access denied' : $e->getMessage()], 404); + return Response::make( + ['error' => !Config::get('app.debug') || empty($e->getMessage()) ? 'Access denied' : $e->getMessage()], + 404); }); API::error(function(\Exception $e) { - return Response::make(['error' => $e->getMessage()], 500); + return Response::make( + ['error' => Config::get('app.debug') ? $e->getMessage() : "Internal error"], + 500); }); } diff --git a/src/CamilStaps/BotleaguesApi/Competition.php b/src/CamilStaps/BotleaguesApi/Competition.php new file mode 100644 index 0000000..a8da665 --- /dev/null +++ b/src/CamilStaps/BotleaguesApi/Competition.php @@ -0,0 +1,12 @@ +<?php +namespace CamilStaps\BotleaguesApi; + +use Illuminate\Database\Eloquent\Model; + +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/CompetitionType.php b/src/CamilStaps/BotleaguesApi/CompetitionType.php new file mode 100644 index 0000000..b9fb426 --- /dev/null +++ b/src/CamilStaps/BotleaguesApi/CompetitionType.php @@ -0,0 +1,13 @@ +<?php +namespace CamilStaps\BotleaguesApi; + +use Illuminate\Database\Eloquent\Model; + +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/Game.php b/src/CamilStaps/BotleaguesApi/Game.php new file mode 100644 index 0000000..3823ab2 --- /dev/null +++ b/src/CamilStaps/BotleaguesApi/Game.php @@ -0,0 +1,11 @@ +<?php +namespace CamilStaps\BotleaguesApi; + +use Illuminate\Database\Eloquent\Model; + +class Game extends Model { + + protected $table = 'games'; + protected $fillable = ['title']; + +}
\ No newline at end of file diff --git a/src/CamilStaps/BotleaguesApi/Participant.php b/src/CamilStaps/BotleaguesApi/Participant.php new file mode 100644 index 0000000..65e1261 --- /dev/null +++ b/src/CamilStaps/BotleaguesApi/Participant.php @@ -0,0 +1,12 @@ +<?php +namespace CamilStaps\BotleaguesApi; + +use Illuminate\Database\Eloquent\Model; + +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/User.php b/src/CamilStaps/BotleaguesApi/User.php index 849ae83..161d285 100644 --- a/src/CamilStaps/BotleaguesApi/User.php +++ b/src/CamilStaps/BotleaguesApi/User.php @@ -9,4 +9,8 @@ class User extends Model { protected $hidden = ['password', 'remember_token', 'api_key']; protected $fillable = ['email', 'password']; + public function isAdministrator() { + return (bool) $this->isAdministrator; + } + }
\ No newline at end of file diff --git a/src/controllers/BotController.php b/src/controllers/BotController.php index c304d1b..9349ea8 100644 --- a/src/controllers/BotController.php +++ b/src/controllers/BotController.php @@ -3,16 +3,26 @@ namespace CamilStaps\BotleaguesApi; class BotController extends BaseController { + protected $bot; + + public function __construct(Bot $bot) { + $this->bot = $bot; + } + public function index() { - return Bot::all(); + return $this->bot->all(); } public function show($id) { - return Bot::findOrFail($id); + return $this->bot->findOrFail($id); + } + + public function update($id) { + throw new \Exception("Not implemented yet."); } - public function edit($id) { - return $this->response->noContent(); + 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..6529433 --- /dev/null +++ b/src/controllers/CompetitionController.php @@ -0,0 +1,36 @@ +<?php +namespace CamilStaps\BotleaguesApi; + +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..430f7b5 --- /dev/null +++ b/src/controllers/CompetitionTypeController.php @@ -0,0 +1,36 @@ +<?php +namespace CamilStaps\BotleaguesApi; + +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..3ef9c37 --- /dev/null +++ b/src/controllers/GameController.php @@ -0,0 +1,36 @@ +<?php +namespace CamilStaps\BotleaguesApi; + +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..2ca28fb --- /dev/null +++ b/src/controllers/ParticipantController.php @@ -0,0 +1,36 @@ +<?php +namespace CamilStaps\BotleaguesApi; + +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/UserController.php b/src/controllers/UserController.php index 02400e9..03bebbc 100644 --- a/src/controllers/UserController.php +++ b/src/controllers/UserController.php @@ -4,7 +4,6 @@ namespace CamilStaps\BotleaguesApi; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Input; -use Illuminate\Support\Facades\Redirect; class UserController extends BaseController { @@ -12,6 +11,8 @@ class UserController extends BaseController { public function __construct(User $user) { $this->user = $user; + + $this->beforeFilter('current_user', array('only' => ['update', 'destroy'])); } public function index() { @@ -23,10 +24,6 @@ class UserController extends BaseController { } public function update($id) { - if ($id != Auth::user()->id) { - throw new \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException(); - } - $s = $this->user->find($id); if (!empty(Input::get('password'))) @@ -41,7 +38,7 @@ class UserController extends BaseController { public function store() { $this->user->email = Input::get('email'); - $this->user->password = Hash::make(Input::get('password')); + $this->user->password = empty(Input::get('password')) ? null : Hash::make(Input::get('password')); if ($this->user->save()) { return $this->response->created(); diff --git a/src/filters.php b/src/filters.php index b3d9bbc..11f0096 100644 --- a/src/filters.php +++ b/src/filters.php @@ -1 +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/migrations/2015_02_23_184402_botleaguesapi-create_users.php b/src/migrations/2015_02_23_184402_botleaguesapi-create_users.php index b768180..b054a43 100644 --- a/src/migrations/2015_02_23_184402_botleaguesapi-create_users.php +++ b/src/migrations/2015_02_23_184402_botleaguesapi-create_users.php @@ -18,6 +18,7 @@ class BotleaguesapiCreateUsers extends Migration { $table->string('email', 127)->unique(); $table->string('password', 60); $table->rememberToken(); + $table->boolean('isAdministrator')->default(false); $table->timestamps(); }); } 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/routes.php b/src/routes.php index 707bf06..5d7b8b6 100644 --- a/src/routes.php +++ b/src/routes.php @@ -1,9 +1,50 @@ <?php Route::group(array('https'), function() { + + Route::api(['version' => 'v1', 'protected' => false], function () { + + Route::resource('bot', 'CamilStaps\BotleaguesApi\BotController', + ['only' => ['index','show']]); + + Route::resource('competition', 'CamilStaps\BotleaguesApi\CompetitionController', + ['only' => ['index','show']]); + + Route::resource('competition_type', 'CamilStaps\BotleaguesApi\CompetitionTypeController', + ['only' => ['index','show']]); + + Route::resource('game', 'CamilStaps\BotleaguesApi\GameController', + ['only' => ['index','show']]); + + Route::resource('participant', 'CamilStaps\BotleaguesApi\ParticipantController', + ['only' => ['index','show']]); + + Route::resource('user', 'CamilStaps\BotleaguesApi\UserController', + ['only' => ['index','show','store']]); + + }); + Route::api(['version' => 'v1', 'protected' => true, 'providers' => 'basic'], function () { + Route::resource('bot', 'CamilStaps\BotleaguesApi\BotController', - array('except' => ['create','edit'])); + ['except' => ['index', 'show', 'create','edit']]); + Route::resource('user', 'CamilStaps\BotleaguesApi\UserController', - array('except' => ['create','edit'])); + ['except' => ['index', 'show', 'create','edit','store']]); + //'before' => 'current_user']); + + Route::group(array('before' => 'administrator'), function() { + + Route::resource('competition', 'CamilStaps\BotleaguesApi\CompetitionController', + ['except' => ['index', 'show', 'create', 'edit']]); + + Route::resource('competition_type', 'CamilStaps\BotleaguesApi\CompetitionTypeController', + ['except' => ['index', 'show', 'create', 'edit']]); + + Route::resource('game', 'CamilStaps\BotleaguesApi\GameController', + ['except' => ['index', 'show', 'create','edit']]); + + }); + }); + });
\ No newline at end of file |