From 03d9e3313fb01033a95e3d09a75be3482cabf032 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Sun, 10 May 2015 18:56:16 +0300 Subject: UserTokens --- src/CamilStaps/BotleaguesApi/UserToken.php | 18 +++++++++++ src/controllers/UserTokenController.php | 34 ++++++++++++++++++++ ..._10_134654_botleaguesapi-create_user_tokens.php | 36 ++++++++++++++++++++++ src/routes.php | 3 ++ 4 files changed, 91 insertions(+) create mode 100644 src/CamilStaps/BotleaguesApi/UserToken.php create mode 100644 src/controllers/UserTokenController.php create mode 100644 src/migrations/2015_05_10_134654_botleaguesapi-create_user_tokens.php (limited to 'src') diff --git a/src/CamilStaps/BotleaguesApi/UserToken.php b/src/CamilStaps/BotleaguesApi/UserToken.php new file mode 100644 index 0000000..02a8891 --- /dev/null +++ b/src/CamilStaps/BotleaguesApi/UserToken.php @@ -0,0 +1,18 @@ +valid_till = date("Y-m-d H:i:s", time() + 3600); + + return parent::save($options); + } + +} \ No newline at end of file diff --git a/src/controllers/UserTokenController.php b/src/controllers/UserTokenController.php new file mode 100644 index 0000000..d894b52 --- /dev/null +++ b/src/controllers/UserTokenController.php @@ -0,0 +1,34 @@ +userToken = $userToken; + } + + public function index() { + return $this->userToken->where('userId', '=', Auth::user()->id)->get(); + } + + public function show($id) { + return $this->userToken->where('userId', '=', Auth::user()->id)->findOrFail($id); + } + + public function store() { + $this->userToken->userId = Auth::user()->id; + $this->userToken->token = sha1(mt_rand()); + + if ($this->userToken->save()) { + return $this->userToken; + } else { + throw new Dingo\Api\Exception\StoreResourceFailedException; + } + } + +} \ No newline at end of file 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 @@ +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/routes.php b/src/routes.php index 027603c..4935f7c 100644 --- a/src/routes.php +++ b/src/routes.php @@ -31,6 +31,9 @@ Route::group(array('https'), function() { Route::resource('user', 'CamilStaps\BotleaguesApi\UserController', ['except' => ['index', 'show', 'create','edit','store']]); + + Route::resource('user_token', 'CamilStaps\BotleaguesApi\UserTokenController', + ['only' => ['index', 'show', 'store']]); Route::group(array('before' => 'administrator'), function() { -- cgit v1.2.3