Completed
Push — master ( 8ffaa6...f544e7 )
by Nicklas
02:28
created

User::controlAuthority()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Nicklas\Comment\Modules;
4
5
/**
6
 * A database driven model.
7
 */
8
class User extends ActiveRecordModelExtender
9
{
10
11
        /**
12
     * @var string $tableName name of the database table.
13
     */
14
    protected $tableName = "ramverk1_users";
15
16
    /**
17
     * Columns in the table.
18
     *
19
     * @var integer $id primary key auto incremented.
20
     */
21
    public $id;
22
    public $name;
23
    public $email;
24
    public $pass;
25
    public $authority = "user";
26
    public $question;
27
28
    /**
29
     * Check if user exists
30
     *
31
     * @param string $name
32
     *
33
     * @return boolean true if user exists in database else false
34
     */
35 1
    public function userExists($name)
36
    {
37 1
        $user = $this->find("name", $name);
38 1
        return !$user ? false : true;
39
    }
40
    /**
41
     * Returns gravatar link
42
     *
43
     * @param string $email
0 ignored issues
show
Bug introduced by
There is no parameter named $email. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
44
     *
45
     * @return string as gravatar link
46
     */
47 5
    public function setGravatar()
48
    {
49 5
        $this->img = $this->gravatar($this->email);
0 ignored issues
show
Bug introduced by
The property img does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
50 5
    }
51
52
    /**
53
     * Returns gravatar link
54
     *
55
     * @param string $name
56
     *
57
     * @return string as gravatar link
58
     */
59 4
    public function getGravatar($name)
60
    {
61 4
        $this->find("name", $name);
62 4
        return $this->gravatar($this->email);
63
    }
64
65
66
67
68
    /**
69
     * Set the pass.
70
     *
71
     * @param string $pass the pass to use.
72
     *
73
     * @return void
74
     */
75 5
    public function setPassword($pass)
76
    {
77 5
        $this->pass = password_hash($pass, PASSWORD_DEFAULT);
78 5
    }
79
80
    /**
81
     * Verify the name and the pass, if successful the object contains
82
     * all details from the database row.
83
     *
84
     * @param string $name  name to check.
85
     * @param string $pass the pass to use.
86
     *
87
     * @return boolean true if name and pass matches, else false.
88
     */
89 3
    public function verifyPassword($name, $pass)
90
    {
91 3
        $this->find("name", $name);
92 3
        return password_verify($pass, $this->pass);
93
    }
94
95
    /**
96
     * Verify the name and the anaswer, if successful the object contains
97
     * all details from the database row.
98
     *
99
     * @param string $name  name to check.
100
     * @param string $answer the answer.
101
     *
102
     * @return boolean true if name and pass matches, else false.
103
     */
104 1
    public function verifyQuestion($name, $answer)
105
    {
106 1
        $this->find("name", $name);
107 1
        return ($this->question == $answer);
108
    }
109
110
111
    /**
112
    * Check if a post belongs to user
113
    *
114
    *
115
    * @return boolean
116
    */
117 1
    public function controlAuthority($loggedUser, $name)
118
    {
119 1
        $this->find("name", $loggedUser);
120
        // IF AUTHORITY == admin, then continue
121 1
        if ($this->authority != "admin") {
122 1
            return ($this->name == $name);
123
        }
124 1
        return true;
125
    }
126
}
127