Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用者角色與權限 CURD 部分 #66

Merged
merged 8 commits into from
Jun 5, 2019
Merged
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
22 changes: 22 additions & 0 deletions app/Http/Controllers/PermissionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Http\Controllers;

use Spatie\Permission\Models\Permission;

class PermissionController extends Controller
{
use ApiTrait;

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\JsonResponse
*/
public function index()
{
$permissions = Permission::get(['name', 'description']);

return $this->returnSuccess('Success', $permissions);
}
}
89 changes: 89 additions & 0 deletions app/Http/Controllers/RoleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace App\Http\Controllers;

use App\Http\Requests\RoleRequest;
use Spatie\Permission\Models\Role;

class RoleController extends Controller
{
use ApiTrait;
use CheckPermissionTrait;

/**
* RoleController constructor.
*/
public function __construct()
{
$this->checkPermissionApiResource();
}

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\JsonResponse
*/
public function index()
{
$roles = Role::all();

return $this->returnSuccess('Success', $roles);
}

/**
* Store a newly created resource in storage.
*
* @param RoleRequest $request
* @return \Illuminate\Http\JsonResponse
*/
public function store(RoleRequest $request)
{
$role = Role::create($request->only('name'));
$role->syncPermissions($request->input('permissions'));

return $this->returnSuccess('Store Success', $role);
}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\JsonResponse
*/
public function show($id)
{
$role = Role::with('permissions')->find($id);

return $this->returnSuccess('Success', $role);
}

/**
* Update the specified resource in storage.
*
* @param RoleRequest $request
* @param $id
* @return \Illuminate\Http\JsonResponse
*/
public function update(RoleRequest $request, $id)
{
$role = Role::findOrFail($id);
hashman marked this conversation as resolved.
Show resolved Hide resolved
$role->update($request->only('name'));
$role->syncPermissions($request->input('permissions'));

return $this->returnSuccess('Update Success', $role);
}

/**
* Remove the specified resource from storage.
*
* @param $id
* @return \Illuminate\Http\JsonResponse
*/
public function destroy($id)
{
$role = Role::findOrFail($id);
hashman marked this conversation as resolved.
Show resolved Hide resolved
$role->delete();

return $this->returnSuccess('Success', $role);
}
}
16 changes: 11 additions & 5 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public function index(Request $request)
$order_field = $request->input('orderby_field', 'id');
$order_method = $request->input('order_method', 'desc');
$limit = $request->input('limit', 15);
$user = User::orderBy($order_field, $order_method)
$user = User::with('roles')
->orderBy($order_field, $order_method)
->paginate($limit);

return $this->returnSuccess('Success.', $user);
Expand All @@ -42,31 +43,36 @@ public function index(Request $request)
public function store(UserRequest $request)
{
$user = User::create($request->all());
$user->syncRoles($request->input('roles'));

return $this->returnSuccess('Store Success.', $user);
}

/**
* @param User $user
* @param $id
* @return \Illuminate\Http\JsonResponse
*/
public function show(User $user)
public function show($id)
{
$user = User::with('roles')->findOrFail($id);

return $this->returnSuccess('Show Success.', $user);
}

/**
* @param UserRequest $request
* @param User $user
* @param $id
* @return \Illuminate\Http\JsonResponse
*/
public function update(UserRequest $request, User $user)
public function update(UserRequest $request, $id)
{
$user = User::findOrFail($id);
$user->update([
'name' => $request->name,
'email' => $request->email,
'telegram_id' => $request->telegram_id,
]);
$user->syncRoles($request->input('roles'));

return $this->returnSuccess('Show Success.', $user);
}
Expand Down
51 changes: 51 additions & 0 deletions app/Http/Requests/RoleRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace App\Http\Requests;

class RoleRequest extends BaseRequest
{
/**
* 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()
{
if ($this->getMethod() == 'PUT') {
$id = explode('/', $this->path())[2];

return [
'name' => 'required|string|unique:roles,name,' . $id,
'permissions' => 'present|array',
];
}

return [
'name' => 'required|string|unique:roles',
'permissions' => 'present|array',
];
}

/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes()
{
return [
'name' => '名稱',
'permissions' => '權限',
];
}
}
2 changes: 2 additions & 0 deletions app/Http/Requests/UserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ public function rules()
'email' => 'required|email|unique:users,email',
'password_confirmation' => 'required',
'password' => 'required|string|min:8|confirmed',
'roles' => 'present|array',
];
} else {
$id = $path[2];

return [
'name' => 'required|string',
'email' => 'required|email|unique:users,email,' . $id,
'roles' => 'present|array',
];
}
}
Expand Down
6 changes: 6 additions & 0 deletions config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],

'testing' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],

'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
Expand Down
10 changes: 10 additions & 0 deletions database/factories/RoleFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use Faker\Generator as Faker;
use Spatie\Permission\Models\Role;

$factory->define(Role::class, function (Faker $faker) {
return [
'name' => $faker->name,
];
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AddTypeToLoanTable extends Migration
public function up()
{
Schema::table('loans', function ($table) {
$table->unsignedInteger('type')->comment = '類型(0:Equipment,1:RaiseEquipment)';
$table->unsignedInteger('type')->default(0)->comment = '類型(0:Equipment,1:RaiseEquipment)';
hashman marked this conversation as resolved.
Show resolved Hide resolved
});
}

Expand Down
2 changes: 2 additions & 0 deletions database/seeds/RolesAndPermissionsSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public function run()
['name' => 'Barcode:Read', 'description' => '檢視所有 Barcode'],
['name' => 'ImportExport:Read', 'description' => '匯出資料'],
['name' => 'ImportExport:Write', 'description' => '匯入資料'],
['name' => 'Role:Read', 'description' => '檢視角色'],
['name' => 'Role:Write', 'description' => '管理角色'],
];

foreach ($permissions as $permission) {
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
<env name="MAIL_DRIVER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="testing"/>
</php>
</phpunit>
2 changes: 1 addition & 1 deletion resources/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ axios.interceptors.response.use(
response => response,
(error) => {
if (error.response.status === 403) {
router.push('403')
router.push('/403');
}
}
);
Expand Down
16 changes: 13 additions & 3 deletions resources/js/include/Sidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,24 @@
icon: 'fas fa-id-card',
href: '/student-verify'
}, {
title: '設定',
icon: 'fas fa-cogs',
title: '使用者管理',
icon: 'fas fa-users-cog',
child: [
{
href: '/user',
title: '帳號管理',
title: '使用者管理',
icon: 'fas fa-user-secret'
}, {
href: '/user/role',
title: '角色管理',
icon: 'fas fa-id-badge'
}
]
}, {
title: '設定',
icon: 'fas fa-cogs',
child: [
{
href: '/tool/print',
title: '條碼列印',
icon: 'fas fa-barcode'
Expand Down
3 changes: 3 additions & 0 deletions resources/js/pages/telegram_bot/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@
this.getAllMessageInterval = setInterval(function () {
self.getAllMessage();
}, 1000 * 10);
},
destroyed() {
clearInterval(this.getAllMessageInterval);
}
}
</script>
Loading