few minor updates

master
Константин 2023-12-13 13:50:09 +03:00
parent 9911f60b85
commit 401eeced28
6 changed files with 48 additions and 32 deletions

View File

@ -44,6 +44,7 @@ class User extends Authenticatable {
'name', 'name',
'phone', 'phone',
'email', 'email',
'position',
'password' 'password'
]; ];
@ -90,39 +91,21 @@ class User extends Authenticatable {
return collect(explode(' ', $this->name))->slice(1, 2)->join(' '); return collect(explode(' ', $this->name))->slice(1, 2)->join(' ');
} }
public function getIsAdminAttribute(): bool { public function getRoleAttribute() {
return $this->hasRole('Administrator') || $this->isMainCompanyAdmin; return $this->roles()->first();
} }
public function getIsSuperAdminAttribute(): bool { public function getIsAdminAttribute(): bool {
return $this->hasRole('Administrator'); return $this->hasRole('Administrator');
} }
public function getIsModeratorAttribute(): bool { public function getIsEditorAttribute(): bool {
return $this->membership()->where(['role' => CompanyMemberRole::MODERATOR])->mainCompany()->exists(); return $this->hasRole('Editor');
}
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 getPrivilegesAttribute(): array { public function getPrivilegesAttribute(): array {
return [ return [
'super_admin' => $this->isSuperAdmin,
'admin' => $this->isAdmin, 'admin' => $this->isAdmin,
'expert' => $this->isExpert, 'editor' => $this->isEditor
'applications_manager' => $this->isApplicationsManager,
'main_company_member' => $this->isMainCompanyMember
]; ];
} }

View File

@ -2,15 +2,18 @@
namespace App\Services\Forms\Users; namespace App\Services\Forms\Users;
use App\Events\UserRegistered;
use App\Models\Objects\FieldType; use App\Models\Objects\FieldType;
use App\Models\Role;
use App\Models\User; use App\Models\User;
use App\Services\Forms\FormsService; use App\Services\Forms\FormsService;
use App\Transformers\Assets\AssetTransformer; use App\Transformers\Assets\AssetTransformer;
use App\Transformers\Users\RoleTransformer;
use App\Transformers\Users\UserTransformer; use App\Transformers\Users\UserTransformer;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
class UserForms extends FormsService { class UserForms extends FormsService {
public array $formTitles = ['create' => 'Создание нового профиля', 'update' => 'Редактирование профиля']; public array $formTitles = ['create' => 'Создание сотрудника', 'update' => 'Редактирование сотрудника'];
public function form(?string $id = null, array $data = []): array { public function form(?string $id = null, array $data = []): array {
$model = User::byUuid($id)->first(); $model = User::byUuid($id)->first();
@ -27,7 +30,7 @@ class UserForms extends FormsService {
'title' => 'Электронная почта', 'title' => 'Электронная почта',
'type' => FieldType::STRING, 'type' => FieldType::STRING,
'required' => true, 'required' => true,
'readonly' => !!$model, //'readonly' => !!$model,
'value' => $model->email ?? null 'value' => $model->email ?? null
], ],
[ [
@ -37,17 +40,33 @@ class UserForms extends FormsService {
'required' => true, 'required' => true,
'value' => $model->name ?? null 'value' => $model->name ?? null
], ],
[
'name' => 'position',
'title' => 'Должность',
'type' => FieldType::STRING,
'required' => true,
'value' => $model->position ?? null
],
[ [
'name' => 'phone', 'name' => 'phone',
'title' => 'Телефон', 'title' => 'Телефон',
'type' => FieldType::STRING, 'type' => FieldType::STRING,
'value' => $model->phone ?? null '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', 'name' => 'avatar',
'title' => 'Фотография профиля', 'title' => 'Фотография профиля',
'type' => FieldType::IMAGE, '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]; return ['data' => $fields];
@ -57,16 +76,26 @@ class UserForms extends FormsService {
public function store(array $data): ?JsonResponse { public function store(array $data): ?JsonResponse {
$model = User::getByData($data, true); $model = User::getByData($data, true);
$model->syncRoles($data['role'] ?? null);
return fractal($model, new UserTransformer())->respond(); return fractal($model, new UserTransformer())->respond();
} }
public function update(string $id, array $data): ?JsonResponse { public function update(string $id, array $data): ?JsonResponse {
$model = User::byUuid($id)->firstOrFail(); $model = User::byUuid($id)->firstOrFail();
$model->update($data); $model->update(collect($data)->only('name', 'position', 'phone')->all());
$model->setAvatar($data['avatar'] ?? null); $model->setAvatar($data['avatar'] ?? null);
$model->syncRoles($data['role'] ?? null);
$this->checkEmail($model, $data['email'] ?? null);
return fractal($model->fresh(), new UserTransformer())->respond(); 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));
}
}
} }

View File

@ -15,6 +15,7 @@ class RoleTransformer extends TransformerAbstract {
return [ return [
'id' => $model->uuid, 'id' => $model->uuid,
'name' => $model->name, 'name' => $model->name,
'title' => $model->title,
'createdAt' => $model->created_at->toIso8601String(), 'createdAt' => $model->created_at->toIso8601String(),
'updatedAt' => $model->updated_at->toIso8601String() 'updatedAt' => $model->updated_at->toIso8601String()
]; ];

View File

@ -23,6 +23,7 @@ class CreateUsersTable extends Migration
$table->integer('asset_id')->nullable(); $table->integer('asset_id')->nullable();
$table->string('phone')->nullable(); $table->string('phone')->nullable();
$table->string('email')->unique(); $table->string('email')->unique();
$table->string('position', 750)->nullable();
$table->timestamp('email_verified_at')->nullable(); $table->timestamp('email_verified_at')->nullable();
$table->string('password'); $table->string('password');
$table->rememberToken(); $table->rememberToken();

View File

@ -26,6 +26,7 @@ class CreatePermissionTables extends Migration
Schema::create($tableNames['roles'], function (Blueprint $table) { Schema::create($tableNames['roles'], function (Blueprint $table) {
$table->bigIncrements('id'); $table->bigIncrements('id');
$table->string('name'); $table->string('name');
$table->string('title');
$table->string('guard_name'); $table->string('guard_name');
$table->char('uuid', 36)->index()->unique(); $table->char('uuid', 36)->index()->unique();
$table->timestamps(); $table->timestamps();

View File

@ -12,8 +12,9 @@ class RoleTableSeeder extends Seeder
* @var array|\Illuminate\Support\Collection * @var array|\Illuminate\Support\Collection
*/ */
public $roles = [ public $roles = [
['name' => 'Administrator'], ['name' => 'User', 'title' => 'Пользователь'],
['name' => 'User'], ['name' => 'Editor', 'title' => 'Редактор'],
['name' => 'Administrator', 'title' => 'Администратор']
]; ];
/** /**