aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCamil Staps2015-05-10 18:56:16 +0300
committerCamil Staps2015-05-10 18:56:16 +0300
commit03d9e3313fb01033a95e3d09a75be3482cabf032 (patch)
treedd3ce9fdf6a7283b4cec1e1e75727873b7df17cf /src
parentLoginException; RedirectException (diff)
UserTokens
Diffstat (limited to 'src')
-rw-r--r--src/CamilStaps/BotleaguesApi/UserToken.php18
-rw-r--r--src/controllers/UserTokenController.php34
-rw-r--r--src/migrations/2015_05_10_134654_botleaguesapi-create_user_tokens.php36
-rw-r--r--src/routes.php3
4 files changed, 91 insertions, 0 deletions
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 @@
+<?php
+namespace CamilStaps\BotleaguesApi;
+
+use Illuminate\Database\Eloquent\Model;
+
+class UserToken extends Model {
+
+ protected $table = 'user_tokens';
+ protected $hidden = ['token'];
+ protected $fillable = ['userId', 'token', 'valid_till'];
+
+ public function save(array $options = array()) {
+ $this->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 @@
+<?php
+namespace CamilStaps\BotleaguesApi;
+
+use Illuminate\Support\Facades\Input;
+use Illuminate\Support\Facades\Auth;
+
+class UserTokenController extends BaseController {
+
+ protected $userToken;
+
+ public function __construct(UserToken $userToken) {
+ $this->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 @@
+<?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/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() {