few minor updates
parent
9911f60b85
commit
401eeced28
|
|
@ -44,6 +44,7 @@ class User extends Authenticatable {
|
|||
'name',
|
||||
'phone',
|
||||
'email',
|
||||
'position',
|
||||
'password'
|
||||
];
|
||||
|
||||
|
|
@ -90,39 +91,21 @@ class User extends Authenticatable {
|
|||
return collect(explode(' ', $this->name))->slice(1, 2)->join(' ');
|
||||
}
|
||||
|
||||
public function getIsAdminAttribute(): bool {
|
||||
return $this->hasRole('Administrator') || $this->isMainCompanyAdmin;
|
||||
public function getRoleAttribute() {
|
||||
return $this->roles()->first();
|
||||
}
|
||||
public function getIsSuperAdminAttribute(): bool {
|
||||
public function getIsAdminAttribute(): bool {
|
||||
return $this->hasRole('Administrator');
|
||||
}
|
||||
public function getIsModeratorAttribute(): bool {
|
||||
return $this->membership()->where(['role' => CompanyMemberRole::MODERATOR])->mainCompany()->exists();
|
||||
}
|
||||
public function getIsMainCompanyAdminAttribute(): bool {
|
||||
return $this->membership()->where(['role' => CompanyMemberRole::ADMINISTRATOR])->mainCompany()->exists();
|
||||
}
|
||||
public function getIsMainCompanyMemberAttribute(): bool {
|
||||
return $this->companies()->where(['is_main' => 1])->exists();
|
||||
}
|
||||
public function getIsExpertAttribute(): bool {
|
||||
return $this->membership()->mainCompany()->whereHas('objects', function($query) {
|
||||
Field::applyFilters($query, collect(['types' => 'company-member-properties', 'moderate-permissions' => 'applications']));
|
||||
})->exists();
|
||||
}
|
||||
public function getIsApplicationsManagerAttribute(): bool {
|
||||
return $this->membership()->mainCompany()->whereHas('objects', function($query) {
|
||||
Field::applyFilters($query, collect(['types' => 'company-member-properties', 'moderate-permissions' => 'manage-applications']));
|
||||
})->exists();
|
||||
public function getIsEditorAttribute(): bool {
|
||||
return $this->hasRole('Editor');
|
||||
}
|
||||
|
||||
|
||||
public function getPrivilegesAttribute(): array {
|
||||
return [
|
||||
'super_admin' => $this->isSuperAdmin,
|
||||
'admin' => $this->isAdmin,
|
||||
'expert' => $this->isExpert,
|
||||
'applications_manager' => $this->isApplicationsManager,
|
||||
'main_company_member' => $this->isMainCompanyMember
|
||||
'editor' => $this->isEditor
|
||||
];
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,15 +2,18 @@
|
|||
|
||||
namespace App\Services\Forms\Users;
|
||||
|
||||
use App\Events\UserRegistered;
|
||||
use App\Models\Objects\FieldType;
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
use App\Services\Forms\FormsService;
|
||||
use App\Transformers\Assets\AssetTransformer;
|
||||
use App\Transformers\Users\RoleTransformer;
|
||||
use App\Transformers\Users\UserTransformer;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class UserForms extends FormsService {
|
||||
public array $formTitles = ['create' => 'Создание нового профиля', 'update' => 'Редактирование профиля'];
|
||||
public array $formTitles = ['create' => 'Создание сотрудника', 'update' => 'Редактирование сотрудника'];
|
||||
|
||||
public function form(?string $id = null, array $data = []): array {
|
||||
$model = User::byUuid($id)->first();
|
||||
|
|
@ -27,7 +30,7 @@ class UserForms extends FormsService {
|
|||
'title' => 'Электронная почта',
|
||||
'type' => FieldType::STRING,
|
||||
'required' => true,
|
||||
'readonly' => !!$model,
|
||||
//'readonly' => !!$model,
|
||||
'value' => $model->email ?? null
|
||||
],
|
||||
[
|
||||
|
|
@ -37,17 +40,33 @@ class UserForms extends FormsService {
|
|||
'required' => true,
|
||||
'value' => $model->name ?? null
|
||||
],
|
||||
[
|
||||
'name' => 'position',
|
||||
'title' => 'Должность',
|
||||
'type' => FieldType::STRING,
|
||||
'required' => true,
|
||||
'value' => $model->position ?? null
|
||||
],
|
||||
[
|
||||
'name' => 'phone',
|
||||
'title' => 'Телефон',
|
||||
'type' => FieldType::STRING,
|
||||
'value' => $model->phone ?? null
|
||||
],
|
||||
[
|
||||
'name' => 'role',
|
||||
'title' => 'Полномочия',
|
||||
'type' => FieldType::RELATION,
|
||||
'required' => true,
|
||||
'appearance' => 'radio',
|
||||
'options' => fractal(Role::all(), new RoleTransformer()),
|
||||
'value' => ($v = $model->role ?? null) ? fractal($v, new RoleTransformer()) : null
|
||||
],
|
||||
[
|
||||
'name' => 'avatar',
|
||||
'title' => 'Фотография профиля',
|
||||
'type' => FieldType::IMAGE,
|
||||
'value' => ($model->avatar ?? null) ? fractal($model->avatar, new AssetTransformer()) : null
|
||||
'value' => ($v = $model->avatar ?? null) ? fractal($v, new AssetTransformer()) : null
|
||||
]
|
||||
];
|
||||
return ['data' => $fields];
|
||||
|
|
@ -57,16 +76,26 @@ class UserForms extends FormsService {
|
|||
|
||||
public function store(array $data): ?JsonResponse {
|
||||
$model = User::getByData($data, true);
|
||||
$model->syncRoles($data['role'] ?? null);
|
||||
return fractal($model, new UserTransformer())->respond();
|
||||
|
||||
}
|
||||
|
||||
public function update(string $id, array $data): ?JsonResponse {
|
||||
$model = User::byUuid($id)->firstOrFail();
|
||||
$model->update($data);
|
||||
$model->update(collect($data)->only('name', 'position', 'phone')->all());
|
||||
$model->setAvatar($data['avatar'] ?? null);
|
||||
$model->syncRoles($data['role'] ?? null);
|
||||
$this->checkEmail($model, $data['email'] ?? null);
|
||||
return fractal($model->fresh(), new UserTransformer())->respond();
|
||||
}
|
||||
|
||||
public function checkEmail(User $model, $email) {
|
||||
if ($email && ($email !== $model->email) && !User::byEmail($email)->withTrashed()->exists()) {
|
||||
$model->update(['email' => $email]);
|
||||
$password = User::makeDefaultPassword();
|
||||
$model->setPassword($password);
|
||||
event(new UserRegistered($model, $password));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ class RoleTransformer extends TransformerAbstract {
|
|||
return [
|
||||
'id' => $model->uuid,
|
||||
'name' => $model->name,
|
||||
'title' => $model->title,
|
||||
'createdAt' => $model->created_at->toIso8601String(),
|
||||
'updatedAt' => $model->updated_at->toIso8601String()
|
||||
];
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ class CreateUsersTable extends Migration
|
|||
$table->integer('asset_id')->nullable();
|
||||
$table->string('phone')->nullable();
|
||||
$table->string('email')->unique();
|
||||
$table->string('position', 750)->nullable();
|
||||
$table->timestamp('email_verified_at')->nullable();
|
||||
$table->string('password');
|
||||
$table->rememberToken();
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ class CreatePermissionTables extends Migration
|
|||
Schema::create($tableNames['roles'], function (Blueprint $table) {
|
||||
$table->bigIncrements('id');
|
||||
$table->string('name');
|
||||
$table->string('title');
|
||||
$table->string('guard_name');
|
||||
$table->char('uuid', 36)->index()->unique();
|
||||
$table->timestamps();
|
||||
|
|
|
|||
|
|
@ -12,8 +12,9 @@ class RoleTableSeeder extends Seeder
|
|||
* @var array|\Illuminate\Support\Collection
|
||||
*/
|
||||
public $roles = [
|
||||
['name' => 'Administrator'],
|
||||
['name' => 'User'],
|
||||
['name' => 'User', 'title' => 'Пользователь'],
|
||||
['name' => 'Editor', 'title' => 'Редактор'],
|
||||
['name' => 'Administrator', 'title' => 'Администратор']
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue