added pointer interfaces for jobs
This commit is contained in:
@@ -3,35 +3,17 @@
|
||||
namespace App\Jobs\Auth;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Interfaces\Job\ShouldCreate;
|
||||
use App\Models\Auth\Permission;
|
||||
|
||||
class CreatePermission extends Job
|
||||
class CreatePermission extends Job implements ShouldCreate
|
||||
{
|
||||
protected $permission;
|
||||
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($request)
|
||||
{
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Permission
|
||||
*/
|
||||
public function handle()
|
||||
public function handle(): Permission
|
||||
{
|
||||
\DB::transaction(function () {
|
||||
$this->permission = Permission::create($this->request->all());
|
||||
$this->model = Permission::create($this->request->all());
|
||||
});
|
||||
|
||||
return $this->permission;
|
||||
return $this->model;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,39 +3,21 @@
|
||||
namespace App\Jobs\Auth;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Interfaces\Job\ShouldCreate;
|
||||
use App\Models\Auth\Role;
|
||||
|
||||
class CreateRole extends Job
|
||||
class CreateRole extends Job implements ShouldCreate
|
||||
{
|
||||
protected $role;
|
||||
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($request)
|
||||
{
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Permission
|
||||
*/
|
||||
public function handle()
|
||||
public function handle(): Role
|
||||
{
|
||||
\DB::transaction(function () {
|
||||
$this->role = Role::create($this->request->input());
|
||||
$this->model = Role::create($this->request->input());
|
||||
|
||||
if ($this->request->has('permissions')) {
|
||||
$this->role->permissions()->attach($this->request->get('permissions'));
|
||||
$this->model->permissions()->attach($this->request->get('permissions'));
|
||||
}
|
||||
});
|
||||
|
||||
return $this->role;
|
||||
return $this->model;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,61 +3,43 @@
|
||||
namespace App\Jobs\Auth;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Interfaces\Job\ShouldCreate;
|
||||
use App\Events\Auth\UserCreated;
|
||||
use App\Events\Auth\UserCreating;
|
||||
use App\Models\Auth\User;
|
||||
use Artisan;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
|
||||
class CreateUser extends Job
|
||||
class CreateUser extends Job implements ShouldCreate
|
||||
{
|
||||
protected $user;
|
||||
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($request)
|
||||
{
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Permission
|
||||
*/
|
||||
public function handle()
|
||||
public function handle(): User
|
||||
{
|
||||
event(new UserCreating($this->request));
|
||||
|
||||
\DB::transaction(function () {
|
||||
$this->user = User::create($this->request->input());
|
||||
$this->model = User::create($this->request->input());
|
||||
|
||||
// Upload picture
|
||||
if ($this->request->file('picture')) {
|
||||
$media = $this->getMedia($this->request->file('picture'), 'users');
|
||||
|
||||
$this->user->attachMedia($media, 'picture');
|
||||
$this->model->attachMedia($media, 'picture');
|
||||
}
|
||||
|
||||
if ($this->request->has('dashboards')) {
|
||||
$this->user->dashboards()->attach($this->request->get('dashboards'));
|
||||
$this->model->dashboards()->attach($this->request->get('dashboards'));
|
||||
}
|
||||
|
||||
if ($this->request->has('permissions')) {
|
||||
$this->user->permissions()->attach($this->request->get('permissions'));
|
||||
$this->model->permissions()->attach($this->request->get('permissions'));
|
||||
}
|
||||
|
||||
if ($this->request->has('roles')) {
|
||||
$this->user->roles()->attach($this->request->get('roles'));
|
||||
$this->model->roles()->attach($this->request->get('roles'));
|
||||
}
|
||||
|
||||
if ($this->request->has('companies')) {
|
||||
if (app()->runningInConsole() || request()->isInstall()) {
|
||||
$this->user->companies()->attach($this->request->get('companies'));
|
||||
$this->model->companies()->attach($this->request->get('companies'));
|
||||
} else {
|
||||
$user = user();
|
||||
|
||||
@@ -66,25 +48,25 @@ class CreateUser extends Job
|
||||
});
|
||||
|
||||
if ($companies->isNotEmpty()) {
|
||||
$this->user->companies()->attach($companies->toArray());
|
||||
$this->model->companies()->attach($companies->toArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($this->user->companies)) {
|
||||
if (empty($this->model->companies)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($this->user->companies as $company) {
|
||||
foreach ($this->model->companies as $company) {
|
||||
Artisan::call('user:seed', [
|
||||
'user' => $this->user->id,
|
||||
'user' => $this->model->id,
|
||||
'company' => $company->id,
|
||||
]);
|
||||
}
|
||||
});
|
||||
|
||||
event(new UserCreated($this->user, $this->request));
|
||||
event(new UserCreated($this->model, $this->request));
|
||||
|
||||
return $this->user;
|
||||
return $this->model;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,30 +3,14 @@
|
||||
namespace App\Jobs\Auth;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Interfaces\Job\ShouldDelete;
|
||||
|
||||
class DeletePermission extends Job
|
||||
class DeletePermission extends Job implements ShouldDelete
|
||||
{
|
||||
protected $permission;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $permission
|
||||
*/
|
||||
public function __construct($permission)
|
||||
{
|
||||
$this->permission = $permission;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return boolean|Exception
|
||||
*/
|
||||
public function handle()
|
||||
public function handle(): bool
|
||||
{
|
||||
\DB::transaction(function () {
|
||||
$this->permission->delete();
|
||||
$this->model->delete();
|
||||
});
|
||||
|
||||
return true;
|
||||
|
||||
@@ -3,32 +3,16 @@
|
||||
namespace App\Jobs\Auth;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Interfaces\Job\ShouldDelete;
|
||||
|
||||
class DeleteRole extends Job
|
||||
class DeleteRole extends Job implements ShouldDelete
|
||||
{
|
||||
protected $role;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $role
|
||||
*/
|
||||
public function __construct($role)
|
||||
{
|
||||
$this->role = $role;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return boolean|Exception
|
||||
*/
|
||||
public function handle()
|
||||
public function handle(): bool
|
||||
{
|
||||
\DB::transaction(function () {
|
||||
$this->role->delete();
|
||||
$this->model->delete();
|
||||
|
||||
$this->role->flushCache();
|
||||
$this->model->flushCache();
|
||||
});
|
||||
|
||||
return true;
|
||||
|
||||
@@ -3,34 +3,18 @@
|
||||
namespace App\Jobs\Auth;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Interfaces\Job\ShouldDelete;
|
||||
|
||||
class DeleteUser extends Job
|
||||
class DeleteUser extends Job implements ShouldDelete
|
||||
{
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $user
|
||||
*/
|
||||
public function __construct($user)
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return boolean|Exception
|
||||
*/
|
||||
public function handle()
|
||||
public function handle(): bool
|
||||
{
|
||||
$this->authorize();
|
||||
|
||||
\DB::transaction(function () {
|
||||
$this->user->delete();
|
||||
$this->model->delete();
|
||||
|
||||
$this->user->flushCache();
|
||||
$this->model->flushCache();
|
||||
});
|
||||
|
||||
return true;
|
||||
@@ -38,13 +22,11 @@ class DeleteUser extends Job
|
||||
|
||||
/**
|
||||
* Determine if this action is applicable.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function authorize()
|
||||
public function authorize(): void
|
||||
{
|
||||
// Can't delete yourself
|
||||
if ($this->user->id == user()->id) {
|
||||
if ($this->model->id == user()->id) {
|
||||
$message = trans('auth.error.self_delete');
|
||||
|
||||
throw new \Exception($message);
|
||||
|
||||
@@ -3,37 +3,17 @@
|
||||
namespace App\Jobs\Auth;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Interfaces\Job\ShouldUpdate;
|
||||
use App\Models\Auth\Permission;
|
||||
|
||||
class UpdatePermission extends Job
|
||||
class UpdatePermission extends Job implements ShouldUpdate
|
||||
{
|
||||
protected $permission;
|
||||
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $permission
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($permission, $request)
|
||||
{
|
||||
$this->permission = $permission;
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Permission
|
||||
*/
|
||||
public function handle()
|
||||
public function handle(): Permission
|
||||
{
|
||||
\DB::transaction(function () {
|
||||
$this->permission->update($this->request->all());
|
||||
$this->model->update($this->request->all());
|
||||
});
|
||||
|
||||
return $this->permission;
|
||||
return $this->model;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,41 +3,21 @@
|
||||
namespace App\Jobs\Auth;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Interfaces\Job\ShouldUpdate;
|
||||
use App\Models\Auth\Role;
|
||||
|
||||
class UpdateRole extends Job
|
||||
class UpdateRole extends Job implements ShouldUpdate
|
||||
{
|
||||
protected $role;
|
||||
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $role
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($role, $request)
|
||||
{
|
||||
$this->role = $role;
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Role
|
||||
*/
|
||||
public function handle()
|
||||
public function handle(): Role
|
||||
{
|
||||
\DB::transaction(function () {
|
||||
$this->role->update($this->request->all());
|
||||
$this->model->update($this->request->all());
|
||||
|
||||
if ($this->request->has('permissions')) {
|
||||
$this->role->permissions()->sync($this->request->get('permissions'));
|
||||
$this->model->permissions()->sync($this->request->get('permissions'));
|
||||
}
|
||||
});
|
||||
|
||||
return $this->role;
|
||||
return $this->model;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,34 +3,14 @@
|
||||
namespace App\Jobs\Auth;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Interfaces\Job\ShouldUpdate;
|
||||
use App\Events\Auth\UserUpdated;
|
||||
use App\Events\Auth\UserUpdating;
|
||||
use App\Models\Auth\User;
|
||||
|
||||
class UpdateUser extends Job
|
||||
class UpdateUser extends Job implements ShouldUpdate
|
||||
{
|
||||
protected $user;
|
||||
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $user
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($user, $request)
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return User
|
||||
*/
|
||||
public function handle()
|
||||
public function handle(): User
|
||||
{
|
||||
$this->authorize();
|
||||
|
||||
@@ -40,25 +20,25 @@ class UpdateUser extends Job
|
||||
unset($this->request['password_confirmation']);
|
||||
}
|
||||
|
||||
event(new UserUpdating($this->user, $this->request));
|
||||
event(new UserUpdating($this->model, $this->request));
|
||||
|
||||
\DB::transaction(function () {
|
||||
$this->user->update($this->request->input());
|
||||
$this->model->update($this->request->input());
|
||||
|
||||
// Upload picture
|
||||
if ($this->request->file('picture')) {
|
||||
$media = $this->getMedia($this->request->file('picture'), 'users');
|
||||
|
||||
$this->user->attachMedia($media, 'picture');
|
||||
$this->model->attachMedia($media, 'picture');
|
||||
}
|
||||
|
||||
if ($this->request->has('roles')) {
|
||||
$this->user->roles()->sync($this->request->get('roles'));
|
||||
$this->model->roles()->sync($this->request->get('roles'));
|
||||
}
|
||||
|
||||
if ($this->request->has('companies')) {
|
||||
if (app()->runningInConsole() || request()->isInstall()) {
|
||||
$this->user->companies()->sync($this->request->get('companies'));
|
||||
$this->model->companies()->sync($this->request->get('companies'));
|
||||
} else {
|
||||
$user = user();
|
||||
|
||||
@@ -67,30 +47,28 @@ class UpdateUser extends Job
|
||||
});
|
||||
|
||||
if ($companies->isNotEmpty()) {
|
||||
$this->user->companies()->sync($companies->toArray());
|
||||
$this->model->companies()->sync($companies->toArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->user->contact) {
|
||||
$this->user->contact->update($this->request->input());
|
||||
if ($this->model->contact) {
|
||||
$this->model->contact->update($this->request->input());
|
||||
}
|
||||
});
|
||||
|
||||
event(new UserUpdated($this->user, $this->request));
|
||||
event(new UserUpdated($this->model, $this->request));
|
||||
|
||||
return $this->user;
|
||||
return $this->model;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if this action is applicable.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function authorize()
|
||||
public function authorize(): void
|
||||
{
|
||||
// Can't disable yourself
|
||||
if (($this->request->get('enabled', 1) == 0) && ($this->user->id == user()->id)) {
|
||||
if (($this->request->get('enabled', 1) == 0) && ($this->model->id == user()->id)) {
|
||||
$message = trans('auth.error.self_disable');
|
||||
|
||||
throw new \Exception($message);
|
||||
|
||||
Reference in New Issue
Block a user