Skip to content

Commit bef9dbe

Browse files
authored
Merge pull request #28 from Xatta-Trone/Feat/add-user-wise-dept
add dept access
2 parents 2e5eef8 + 784a009 commit bef9dbe

File tree

5 files changed

+56
-8
lines changed

5 files changed

+56
-8
lines changed

app/Http/Controllers/Api/Admin/UserController.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,16 @@ public function update(UserUpdateRequest $request, $id)
126126
return $this->noPermissionResponse();
127127
}
128128

129-
130129
$user = User::findOrFail($id);
131130
$userOld = $user->replicate();
132131

133-
$user->update($request->validated());
132+
$dept_access = array_merge($request->dept_access ? $request->dept_access : [], [$user->department]);
133+
$dept_access = implode(',', $dept_access);
134+
$data = array_merge($request->validated(), ['dept_access' => $dept_access]);
135+
136+
// dd($dept_access, $user->department, $data);
137+
138+
$user->update($data);
134139
$this->saveAdminUpdateActivity($user->id, 'user', $user->student_id, $userOld, $user->getChanges());
135140
// dd($user);
136141
if ($user) {

app/Http/Controllers/Api/User/MaterialController.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,16 @@ public function courses()
3535
public function infos($deptSlug, $ltSlug = null, $courseSlug = null)
3636
{
3737
// check if can access dept
38-
$dept = Department::select('can_be_accessed_by')->where('slug', $deptSlug)->first();
39-
$canBeAccessedBy = explode(',', $dept->can_be_accessed_by);
38+
// $dept = Department::select('can_be_accessed_by')->where('slug', $deptSlug)->first();
39+
// $canBeAccessedBy = explode(',', $dept->can_be_accessed_by);
40+
41+
$user = auth()->user();
42+
$userDepartments = $user->dept_access ? explode(',', $user->dept_access) : [];
43+
$defaultDepartments = explode(',', Department::query()
44+
->select('can_be_accessed_by')
45+
->where('code', substr($user->student_id, 2, 2))
46+
->get()->first()->can_be_accessed_by);
47+
$canBeAccessedBy = array_unique(array_merge($defaultDepartments, $userDepartments));
4048

4149
if (!in_array(auth()->user()->department, $canBeAccessedBy)) {
4250
return $this->errorResponse('NOT_AUTHORIZED_TO_ACCESS_THIS_DEPARTMENT', []);

app/Http/Requests/UserUpdateRequest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ public function rules()
3131
'status' => ['required'],
3232
'user_letter' => ['required'],
3333
'whitelisted' => ['required'],
34-
'max_devices' => ['required', 'integer']
34+
'max_devices' => ['required', 'integer'],
35+
'dept_access' => ['nullable', 'array'],
3536
];
3637
}
37-
}
38+
39+
40+
}

app/Models/User/User.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ protected static function boot()
3535
}
3636

3737

38-
3938
/**
4039
* The attributes that are mass assignable.
4140
*
@@ -50,7 +49,8 @@ protected static function boot()
5049
'whitelisted',
5150
'deleted_at',
5251
'status',
53-
'max_devices'
52+
'max_devices',
53+
'dept_access',
5454
];
5555

5656
protected $guarded = [];
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class AddDeptAccessToUsersTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('users', function (Blueprint $table) {
17+
$table->string('dept_access')->nullable();
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function down()
27+
{
28+
Schema::table('users', function (Blueprint $table) {
29+
$table->dropColumn('dept_access');
30+
});
31+
}
32+
}

0 commit comments

Comments
 (0)