Skip to content

Commit

Permalink
Merge pull request laravel#37 from eurides-eu/feature/add-user-authen…
Browse files Browse the repository at this point in the history
…tication-endpoints

Add user + client credentials authentication endpoints
  • Loading branch information
jaureguivictoria authored May 9, 2018
2 parents 46ba9c6 + b15a60f commit e8e01bd
Show file tree
Hide file tree
Showing 30 changed files with 920 additions and 113 deletions.
52 changes: 52 additions & 0 deletions app/Console/Commands/Organizations/CreateOrganization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Console\Commands\Organizations;

use App\Organizations\Commands\CreateOrganization as CreateOrganizationCommand;
use Illuminate\Console\Command;
use Madewithlove\Tactician\Traits\DispatchesJobs;

class CreateOrganization extends Command
{
use DispatchesJobs;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'eurides:organization:create
{name : The organization name}
{reference? : The organization internal reference (optional)}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create an organization. Arguments: {name} {reference?}';

/**
* Create a new command instance.
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$name = $this->argument('name');

$referenceCode = $this->argument('reference');

$organization = $this->dispatch(new CreateOrganizationCommand($name, $referenceCode));

$this->info("Organization created successfully => ID {$organization->id}");
}
}
50 changes: 50 additions & 0 deletions app/Console/Commands/Users/CreateUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Console\Commands\Users;

use App\Users\Commands\CreateUser as CreateUserCommand;
use Illuminate\Console\Command;
use Madewithlove\Tactician\Traits\DispatchesJobs;

class CreateUser extends Command
{
use DispatchesJobs;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'eurides:user:create {name} {email} {organization_id}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a user for an organization. Arguments: {name} {email} {organization_id}';

/**
* Create a new command instance.
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$name = $this->argument('name');
$email = $this->argument('email');
$organizationId = $this->argument('organization_id');

$user = $this->dispatch(new CreateUserCommand($name, $email, $organizationId));

$this->info("User created successfully => ID {$user->id}");
}
}
2 changes: 2 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Kernel extends ConsoleKernel
* @var array
*/
protected $commands = [
Commands\Organizations\CreateOrganization::class,
Commands\Users\CreateUser::class,
CreateOAuthClient::class,
];

Expand Down
46 changes: 46 additions & 0 deletions app/Http/Controllers/oAuth/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Http\Controllers\oAuth;

use App\Http\Controllers\Controller;
use App\Http\Requests\oAuth\Users\AuthorizeFromCodeRequest;
use App\Http\Requests\oAuth\Users\SendLoginCodeRequest;
use App\Users\Commands\AuthorizeFromLoginCode;
use App\Users\Commands\IssueLoginCode;

class UserController extends Controller
{
/**
* Get personal access token for this user and send via email.
*
* @param SendLoginCodeRequest $request
*
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
*/
public function sendLoginCode(SendLoginCodeRequest $request)
{
$email = $request->get('email');

$command = new IssueLoginCode($email);

$this->dispatch($command);

return response([]);
}

/**
* Validate active login code and return an access token.
*
* @param AuthorizeFromCodeRequest $request
*
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
*/
public function authorizeFromCode(AuthorizeFromCodeRequest $request)
{
$code = $request->get('code');

$accessToken = $this->dispatch(new AuthorizeFromLoginCode($code));

return response(['accessToken' => $accessToken]);
}
}
2 changes: 2 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;
use Laravel\Passport\Http\Middleware\CheckClientCredentials;

class Kernel extends HttpKernel
{
Expand Down Expand Up @@ -59,5 +60,6 @@ class Kernel extends HttpKernel
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'client' => CheckClientCredentials::class,
];
}
31 changes: 31 additions & 0 deletions app/Http/Requests/oAuth/Users/AuthorizeFromCodeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Requests\oAuth\Users;

use App\Rules\ActiveLoginCode;
use Illuminate\Foundation\Http\FormRequest;

class AuthorizeFromCodeRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'code' => ['string', 'required', new ActiveLoginCode()],
];
}
}
30 changes: 30 additions & 0 deletions app/Http/Requests/oAuth/Users/SendLoginCodeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Requests\oAuth\Users;

use Illuminate\Foundation\Http\FormRequest;

class SendLoginCodeRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'email' => 'string|email|required|exists:users,email',
];
}
}
37 changes: 37 additions & 0 deletions app/Mail/UserLoginCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class UserLoginCode extends Mailable
{
use Queueable, SerializesModels;

/**
* @var string
*/
public $code;

/**
* Create a new message instance.
*/
public function __construct($code)
{
$this->code = $code;
}

/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('emails.user_login')
->with('code', $this->code)
->with('link', config('eurides.app_user_login_url'));
}
}
48 changes: 48 additions & 0 deletions app/Models/LoginCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Models;

use App\Traits\UuidModel;
use App\User;
use Illuminate\Database\Eloquent\Model;

class LoginCode extends Model
{
use UuidModel;

/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'code',
'user_id',
'expired_at',
];

/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'created_at',
'expired_at',
];

/**
* Get the user that owns the code.
*/
public function user()
{
return $this->belongsTo(User::class);
}
}
17 changes: 17 additions & 0 deletions app/Organizations/CommandHandlers/CreateOrganization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Organizations\CommandHandlers;

use App\Models\Organization;
use App\Organizations\Commands\CreateOrganization as Job;

class CreateOrganization
{
public function handle(Job $job)
{
return Organization::create([
'name' => $job->getName(),
'reference_code' => $job->getReferenceCode(),
]);
}
}
44 changes: 44 additions & 0 deletions app/Organizations/Commands/CreateOrganization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Organizations\Commands;

class CreateOrganization
{
/**
* @var string
*/
protected $name;

/**
* @var string
*/
protected $referenceCode;

/**
* CreateOrganization constructor.
*
* @param $name
* @param $referenceCode
*/
public function __construct($name, $referenceCode)
{
$this->name = $name;
$this->referenceCode = $referenceCode;
}

/**
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* @return string
*/
public function getReferenceCode()
{
return $this->referenceCode;
}
}
Loading

0 comments on commit e8e01bd

Please sign in to comment.