Completed
Push — master ( d7caaf...048f0c )
by Mahmoud
04:10
created

Request::hasAccess()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 1
1
<?php
2
3
namespace App\Ship\Parents\Requests;
4
5
use App\Containers\Authorization\Traits\AuthorizationTrait;
6
use App\Ship\Engine\Traits\HashIdTrait;
7
use Illuminate\Foundation\Http\FormRequest as LaravelFormRequest;
8
9
/**
10
 * Class Request
11
 *
12
 * A.K.A (app/Http/Requests/Request.php)
13
 *
14
 * @author  Mahmoud Zalt  <[email protected]>
15
 */
16
abstract class Request extends LaravelFormRequest
17
{
18
    use RequestTrait;
19
    use HashIdTrait;
20
21
    /**
22
     * check if a user has permission to perform an action.
23
     * User can set multiple permissions (separated with "|") and if the user has
24
     * any of the permissions, he will be authorize to proceed with this action.
25
     *
26
     * @return  bool
27
     */
28
    public function hasAccess(User $user = null)
29
    {
30
        // if not in parameters, take from the request object {$this}
31
        $user = $user ? : $this->user();
32
33
        $hasAccess = array_merge(
34
            $this->hasAnyPermissionAccess($user),
35
            $this->hasAnyRoleAccess($user)
36
        );
37
38
        // allow access if user has access to any of the defined roles or permissions.
39
        return empty($hasAccess) ? true : in_array(true, $hasAccess);
40
    }
41
42
    /**
43
     * Check if the submitted ID (mainly URL ID's) is the same as
44
     * the authenticated user ID (based on the user Token).
45
     *
46
     * @return  bool
47
     */
48
    public function isOwner()
49
    {
50
        return $this->user()->id == $this->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Ship\Parents\Requests\Request>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
51
    }
52
53
}
54