Completed
Push — master ( 03841e...c8ea4b )
by Sherif
05:27
created

UserRepository::getModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace App\Modules\Acl\Repositories;
2
3
use App\Modules\Core\AbstractRepositories\AbstractRepository;
4
5
class UserRepository extends AbstractRepository
6
{
7
	/**
8
	 * Return the model full namespace.
9
	 * 
10
	 * @return string
11
	 */
12
	protected function getModel()
13
	{
14
		return 'App\Modules\Acl\AclUser';
15
	}
16
17
	/**
18
	 * Check if the logged in user or the given user 
19
	 * has the given permissions on the given model.
20
	 * 
21
	 * @param  string  $nameOfPermission
22
	 * @param  string  $model            
23
	 * @param  boolean $user
24
	 * @return boolean
25
	 */
26
	public function can($nameOfPermission, $model, $user = false )
27
	{		
28
		$user        = $user ?: \Auth::user();
29
		$permissions = [];
30
		\Core::users()->find($user->id, ['groups.permissions'])->groups->lists('permissions')->each(function ($permission) use (&$permissions, $model){
31
			$permissions = array_merge($permissions, $permission->where('model', $model)->lists('name')->toArray()); 
32
		});
33
		
34
		return in_array($nameOfPermission, $permissions);
35
	}
36
37
	/**
38
	 * Check if the logged in user has the given group.
39
	 * 
40
	 * @param  string  $groupName
41
	 * @return boolean
42
	 */
43
	public function hasGroup($groupName)
44
	{
45
		$groups = \Core::users()->find(\Auth::user()->id)->groups;
46
		return $groups->lists('name')->search($groupName, true);
47
	}
48
49
	/**
50
	 * Assign the given group ids to the given user.
51
	 * 
52
	 * @param  integer $user_id    
53
	 * @param  array   $group_ids
54
	 * @return object
55
	 */
56 View Code Duplication
	public function assignGroups($user_id, $group_ids)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
	{
58
		\DB::transaction(function () use ($user_id, $group_ids) {
59
			$user = \Core::users()->find($user_id);
60
			$user->groups()->detach();
61
			$user->groups()->attach($group_ids);
62
		});
63
64
        return \Core::users()->find($user_id);
65
	}
66
}
67