Page MenuHomeDevCentral

D432.id1056.diff
No OneTemporary

D432.id1056.diff

diff --git a/app/Models/User.php b/app/Models/User.php
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -93,4 +93,26 @@
}
return $this->attributes['username'];
}
+
+ /**
+ * Tries to get the local user matching an external source.
+ *
+ * @param string $source_name The name of the source to check
+ * @param string $source_user_id The user ID at the specified source
+ * @param &User If an user has been found, contains that user object
+ * @return bool true if an user has been found; otherwise, false
+ */
+ public static function tryGetFromExternalSource ($source_name, $source_user_id, &$user) {
+ $source = UserExternalSource::where([
+ 'source_name' => $source_name,
+ 'source_user_id' => $source_user_id
+ ])->first();
+
+ if ($source === null) {
+ return false;
+ }
+
+ $user = $source->getUser();
+ return true;
+ }
}
diff --git a/app/Models/UserExternalSource.php b/app/Models/UserExternalSource.php
new file mode 100644
--- /dev/null
+++ b/app/Models/UserExternalSource.php
@@ -0,0 +1,52 @@
+<?php
+
+namespace AuthGrove\Models;
+
+use Illuminate\Database\Eloquent\Model;
+
+class UserExternalSource extends Model {
+
+ /**
+ * The database table used by the model.
+ *
+ * @var string
+ */
+ protected $table = 'users_external_sources';
+
+ /**
+ * The attributes that are mass assignable.
+ *
+ * @var array
+ */
+ protected $fillable = ['source_name', 'source_user_id', 'user_id'];
+
+ /**
+ * The attributes excluded from the model's JSON form.
+ *
+ * @var array
+ */
+ protected $hidden = [];
+
+ /**
+ * Gets fillable but not hidden attributes, plus create/update time.
+ *
+ * @return Array
+ */
+ public function getAttributes () {
+ $attributes = array_diff($this->fillable , $this->hidden);
+ $attributes[] = 'created_at';
+ $attributes[] = 'updated_at';
+ array_unshift($attributes, 'id');
+ return $attributes;
+ }
+
+ /**
+ * Gets user attached to this source.
+ *
+ * @return User
+ */
+ public function getUser () {
+ return User::find($this->user_id);
+ }
+
+}
diff --git a/database/migrations/2016_06_28_034600_create_users_external_sources_table.php b/database/migrations/2016_06_28_034600_create_users_external_sources_table.php
new file mode 100644
--- /dev/null
+++ b/database/migrations/2016_06_28_034600_create_users_external_sources_table.php
@@ -0,0 +1,36 @@
+<?php
+
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateUsersExternalCredentialsTable extends Migration {
+
+ /**
+ * Run the migrations.
+ *
+ * @return void
+ */
+ public function up() {
+ Schema::create('users_external_sources', function(Blueprint $table) {
+ $table->increments('id');
+ $table->string('source_name');
+ $table->string('source_user_id');
+ $table->integer('user_id')->unsigned();
+
+ $table->timestamps();
+ $table->softDeletes();
+
+ $table->foreign('user_id')->references('id')->on('users');
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down() {
+ Schema::drop('users_external_sources');
+ }
+
+}
diff --git a/tests/Models/UserTest.php b/tests/Models/UserTest.php
new file mode 100644
--- /dev/null
+++ b/tests/Models/UserTest.php
@@ -0,0 +1,22 @@
+<?php
+
+use AuthGrove\Models\User;
+
+/**
+ * Test User model.
+ */
+class UsersTest extends TestCase {
+
+ function testTryGetFromExternalSource () {
+ $this->assertFalse(
+ User::tryGetFromExternalSource('phantasialand', 'Fairy Tale Forest', $user)
+ );
+ $this->assertFalse(
+ User::tryGetFromExternalSource(null, null, $user)
+ );
+ $this->assertFalse(
+ User::tryGetFromExternalSource('', 0, $user)
+ );
+ }
+
+}

File Metadata

Mime Type
text/plain
Expires
Sat, Dec 21, 12:10 (17 h, 20 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2309201
Default Alt Text
D432.id1056.diff (3 KB)

Event Timeline