-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #453 from statonlab/magic-link
Magic link
- Loading branch information
Showing
13 changed files
with
352 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Observation; | ||
use App\ShareToken; | ||
use App\User; | ||
use App\Http\Controllers\Traits\Responds; | ||
use Carbon\Carbon; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Str; | ||
|
||
class ShareTokensController extends Controller | ||
{ | ||
use Responds; | ||
|
||
/** | ||
* Attempts to create a share link if the user is authorized to do so. | ||
* | ||
* @param $id | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function share($id, Request $request) | ||
{ | ||
$user = $request->user(); | ||
|
||
$observation = Observation::findOrFail($id); | ||
|
||
if ($observation->user_id !== $user->id) { | ||
return $this->unauthorized(); | ||
} | ||
|
||
$share_token = $this->createShareToken($user, $observation, $request); | ||
|
||
return $this->success( | ||
"https://treesnap.org/observation/$observation?token=$share_token->value" | ||
); | ||
} | ||
|
||
/** | ||
* Creates a token for the share URL. | ||
* | ||
* @param $user | ||
* @param $observation | ||
* @param $request | ||
* @return mixed | ||
*/ | ||
protected function createShareToken($user, $observation, $request) | ||
{ | ||
$rand = Str::random(60); | ||
while (ShareToken::where('value', $rand)->first()) { | ||
// Key already exists so generate a new one | ||
$rand = Str::random(60); | ||
} | ||
|
||
return ShareToken::create([ | ||
'user_id' => $user->id, | ||
'email' => $request->email, | ||
'observation_id' => $observation->id, | ||
'value' => $rand, | ||
'expires_at' => Carbon::now()->addYear(), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class ShareToken extends Model | ||
{ | ||
/** | ||
* Fillable fields. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = [ | ||
'user_id', | ||
'observation_id', | ||
'expired_at', | ||
'value', | ||
]; | ||
|
||
/** | ||
* The attributes that should be mutated to dates. | ||
* | ||
* @var array | ||
*/ | ||
protected $casts = [ | ||
'expired_at' => 'date', | ||
]; | ||
|
||
/** | ||
* Get the related user (sender). | ||
* | ||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo | ||
*/ | ||
public function user() | ||
{ | ||
return $this->belongsTo('App\User'); | ||
} | ||
|
||
/** | ||
* Get the related observation. | ||
* | ||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo | ||
*/ | ||
public function observation() | ||
{ | ||
return $this->belongsTo('App\Observation'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
database/migrations/2019_05_08_175701_create_sharetokens_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateSharetokensTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('share_tokens', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->integer('user_id')->unsigned()->index(); | ||
$table->integer('observation_id')->unsigned()->index(); | ||
$table->string('value'); //->unique(); | ||
$table->timestamp('expired_at'); | ||
$table->timestamps(); | ||
|
||
$table->unique(['observation_id', 'value']); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('share_tokens'); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"/js/vendor.js": "/js/vendor.js?id=da80df012039296cf353", | ||
"/js/app.js": "/js/app.js?id=77c4fd7378b4f0f4e511", | ||
"/js/app.js": "/js/app.js?id=bd3cc394c89877199d74", | ||
"/css/app.css": "/css/app.css?id=076495e51d1dc3726549", | ||
"/js/admin.js": "/js/admin.js?id=75c7242ae22087a5578f", | ||
"/js/manifest.js": "/js/manifest.js?id=a0d339095905614077ad" | ||
"/js/manifest.js": "/js/manifest.js?id=693e2b0facddc2e283d4" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.