Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions app/Http/Controllers/Admin/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,24 @@ public function index(Request $request): View

return view('admin.users', compact('users'));
}

public function edit(User $user)
{
return view('admin.users.edit', compact('user'));
}

public function update(Request $request, User $user)
{
$data = $request->validate([
'name' => 'required|string|max:255',
'github_name' => 'nullable|string|max:255',
]);

$user->update($data);
$user->admin = $request->has('admin');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тк admin в форме - это чекбокс, то стоит его так и назвать.
плюс - валидация. https://laravel.com/docs/12.x/validation#rule-accepted

$user->save();

return redirect()->route('admin.users.index')
->with('success', 'User updated');
}
}
8 changes: 4 additions & 4 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @property string $name
* @property string $email
* @property string $github_name
* @property bool $is_admin
* @property bool $admin
* @property \Illuminate\Support\Carbon|null $email_verified_at
* @property string $password
* @property string|null $remember_token
Expand Down Expand Up @@ -58,7 +58,7 @@ class User extends Authenticatable implements MustVerifyEmail
* @var string[]
*/
protected $fillable = [
'name', 'email', 'password',
'name', 'email', 'password', 'github_name',
];

/**
Expand Down Expand Up @@ -90,7 +90,7 @@ public static function boot()
$user->github_name = null;
$user->remember_token = null;
$user->password = null;
$user->is_admin = false;
$user->admin = false;

$user->save();

Expand Down Expand Up @@ -159,6 +159,6 @@ public function haveRead(Chapter $chapter): bool

public function scopeAdmins(Builder $builder): Builder
{
return $builder->where('is_admin', '=', true);
return $builder->where('admin', '=', true);
}
}
4 changes: 2 additions & 2 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public function boot(): void
$this->registerPolicies();

Gate::before(function (User $user) {
return $user->is_admin ? true : null;
return $user->admin ? true : null;
});

Gate::define('access-admin', function (User $user) {
return $user->is_admin;
return $user->admin;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('is_admin', 'admin');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rnik82 а для чего переименовывать поле?) Оно ведь булево - верно названо?
Где-то я в комментах писал, относилось к текстам интерфейсным, т.е. в админке норм это назвать admin? или is_admin и так далее. Там нет жесткий требований по переводам.
А вот поле старое лучше вернуть

});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('admin', 'is_admin');
});
}
};
2 changes: 1 addition & 1 deletion database/seeders/UsersTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function run(): void
'name' => 'Admin',
'email' => '[email protected]',
'password' => bcrypt('password'),
'is_admin' => true,
'admin' => true,
]);
}
}
2 changes: 2 additions & 0 deletions resources/lang/en/account.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
'current_email' => 'Current email',
'go_to_gravatar' => 'Go to Gravatar.com',
'github_name' => 'GitHub username',
'admin' => 'Admin',
'name' => 'Name',
];
1 change: 1 addition & 0 deletions resources/lang/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
],
'empty' => 'No users found',
'not_available' => 'N/A',
'edit' => 'Edit User',
],
'comments' => [
'title' => 'Comments',
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/ru/account.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
'current_email' => 'Текущий email',
'go_to_gravatar' => 'Перейти на Gravatar.com',
'github_name' => 'Имя на GitHub',
'admin' => 'Админ',
'name' => 'Имя',
];
1 change: 1 addition & 0 deletions resources/lang/ru/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
'email_not_verified' => 'Email не подтвержден',
],
'empty' => 'Пользователи не найдены',
'edit' => 'Редактирование пользователя',
],
'comments' => [
'title' => 'Комментарии',
Expand Down
10 changes: 9 additions & 1 deletion resources/views/admin/users.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<th>{{ __('admin.users.table.email') }}</th>
<th>{{ __('admin.users.table.role') }}</th>
<th>{{ __('admin.users.table.created') }}</th>
<th>{{ __('admin.users.table.actions') }}</th>
</tr>
</thead>
<tbody>
Expand All @@ -50,7 +51,7 @@
<span class="text-break">{{ $user->email }}</span>
</td>
<td>
@if($user->is_admin)
@if($user->admin)
<span class="badge bg-danger">
<i class="bi bi-shield-fill-check"></i> {{ __('admin.users.role.admin') }}
</span>
Expand All @@ -63,6 +64,13 @@
{{ $user->created_at->format('d.m.Y H:i') }}
</small>
</td>
<td>
<a href="{{ route('admin.users.edit', $user) }}"
class="btn btn-sm btn-outline-primary"
title="{{ __('admin.users.edit') }}">
<i class="bi bi-pencil"></i>
</a>
</td>
</tr>
@empty
<tr>
Expand Down
74 changes: 74 additions & 0 deletions resources/views/admin/users/edit.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
@extends('layouts.app')

@section('title', __('admin.users.edit_user', ['name' => $user->name]))

@section('content')
<div class="container">
<div class="row">
<div class="col-md-3">
{{-- Админское меню --}}
<div class="list-group">
<a href="{{ route('admin.users.index') }}"
class="list-group-item list-group-item-action {{ request()->routeIs('admin.users.*') ? 'active' : '' }}">
{{ __('admin.users.title') }}
</a>
<a href="{{ route('admin.comments.index') }}"
class="list-group-item list-group-item-action {{ request()->routeIs('admin.comments.*') ? 'active' : '' }}">
{{ __('admin.comments.title') }}
</a>
<a href="{{ route('admin.solutions.index') }}"
class="list-group-item list-group-item-action {{ request()->routeIs('admin.solutions.*') ? 'active' : '' }}">
{{ __('admin.solutions.title') }}
</a>
</div>
</div>
<div class="col-md-9">
<div class="card">
<div class="card-header">
<h5 class="card-title mb-0">
{{ __('admin.users.edit') }}
</h5>
</div>
<div class="card-body">
{{ html()->modelForm($user, 'PUT', route('admin.users.update', $user))->open() }}

<div class="mb-3">
{{ html()->label(__('account.name'), 'name')->class('form-label') }}
{{ html()->text('name')
->class('form-control')
->required() }}
</div>

<div class="mb-3">
{{ html()->label(__('account.github_name'), 'github_name')->class('form-label') }}
{{ html()->text('github_name')
->class('form-control') }}
</div>

<div class="mb-3">
<div class="form-check">
{{ html()->checkbox('admin')
->class('form-check-input')
->value(1)
->checked($user->admin) }}
{{ html()->label(__('account.admin'), 'admin')
->class('form-check-label') }}
</div>
</div>

<div class="d-flex gap-2">
<button type="submit" class="btn btn-primary">
{{ __('layout.common.save') }}
</button>
<a href="{{ route('admin.users.index') }}" class="btn btn-secondary">
{{ __('layout.common.cancel') }}
</a>
</div>

{{ html()->closeModelForm() }}
</div>
</div>
</div>
</div>
</div>
@endsection
2 changes: 1 addition & 1 deletion resources/views/layouts/_nav.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class="nav-link p-2">{{ __('layout.nav.sicp_read') }}</a></li>
<li class="nav-item"><a href="{{ TemplateHelper::getBookLink($currentLocale) }}"
class="nav-link link-info p-2">{{ __('layout.nav.sicp_book') }}</a></li>
@auth
@if(auth()->user()->is_admin)
@if(auth()->user()->admin)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

давай кстати тут сразу поменяем, не проверку роли, а через проверку политик (@can)

<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle p-2" href="#" id="adminDropdown" role="button"
data-bs-toggle="dropdown" aria-expanded="false">
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
Route::resource('pages', 'PagesController')->only('show');

Route::namespace('Admin')->prefix('admin')->name('admin.')->group(function (): void {
Route::resource('users', 'UserController')->only('index');
Route::resource('users', 'UserController')->only('index', 'edit', 'update');
Route::resource('comments', 'CommentController')->only('index');
Route::resource('solutions', 'SolutionController')->only('index');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ protected function setUp(): void
{
parent::setUp();

$this->adminUser = User::factory()->create(['is_admin' => true]);
$this->regularUser = User::factory()->create(['is_admin' => false]);
$this->adminUser = User::factory()->create(['admin' => true]);
$this->regularUser = User::factory()->create(['admin' => false]);
}

public function testIndexAsAdmin(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ protected function setUp(): void
{
parent::setUp();

$this->adminUser = User::factory()->create(['is_admin' => true]);
$this->regularUser = User::factory()->create(['is_admin' => false]);
$this->adminUser = User::factory()->create(['admin' => true]);
$this->regularUser = User::factory()->create(['admin' => false]);
}

public function testIndexAsAdmin(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Http/Controllers/Admin/UserControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ protected function setUp(): void
{
parent::setUp();

$this->adminUser = User::factory()->create(['is_admin' => true]);
$this->regularUser = User::factory()->create(['is_admin' => false]);
$this->adminUser = User::factory()->create(['admin' => true]);
$this->regularUser = User::factory()->create(['admin' => false]);
}

public function testIndexAsAdmin(): void
Expand Down