Completed
Push — master ( 789e47...5266e6 )
by Ehsan
03:13
created

AbstractAccessList::setApiClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Botonomous;
4
5
use Botonomous\client\ApiClient;
6
use Botonomous\utility\ClassUtility;
7
8
abstract class AbstractAccessList
9
{
10
    /**
11
     * Dependencies.
12
     */
13
    private $request;
14
    private $dictionary;
15
    private $apiClient;
16
    private $classUtility;
17
18
    /**
19
     * @return mixed
20
     */
21 18
    protected function getAccessControlList()
22
    {
23 18
        return $this->getDictionary()->get('access-control');
24
    }
25
26
    /**
27
     * @param $sublistKey
28
     *
29
     * @return mixed
30
     */
31 18
    protected function getSubAccessControlList($sublistKey)
32
    {
33 18
        $list = $this->getAccessControlList();
34
35 18
        if (!isset($list[$sublistKey])) {
36
            /* @noinspection PhpInconsistentReturnPointsInspection */
37 1
            return;
38
        }
39
40 18
        return $list[$sublistKey];
41
    }
42
43
    /**
44
     * @param array $list
45
     *
46
     * @return bool
47
     */
48 9
    protected function isEmailInList(array $list)
49
    {
50
        // get user info
51 9
        $userInfo = $this->getSlackUserInfo();
52
53 9
        return !empty($userInfo) && in_array($userInfo['profile']['email'], $list['userEmail']);
54
    }
55
56
    /**
57
     * Check if email is white listed or black listed
58
     * If userEmail list is not set, return true for whitelist and false for blacklist.
59
     *
60
     * @return bool
61
     */
62 12
    protected function checkEmail()
63
    {
64
        // load the relevant list based on the class name e.g. BlackList or WhiteList
65 12
        $list = $this->getSubAccessControlList($this->getShortClassName());
66
67 12
        if (!isset($list['userEmail'])) {
68
            // if list is not set do not check it
69 3
            return $this->getShortClassName() === 'whitelist' ? true : false;
70
        }
71
72 9
        return $this->isEmailInList($list);
73
    }
74
75
    /**
76
     * @param string $requestKey
77
     * @param $listKey
78
     * @param string $subListKey
79
     *
80
     * @return bool|null
81
     */
82 12
    protected function findInListByRequestKey($requestKey, $listKey, $subListKey)
83
    {
84
        /**
85
         * load the relevant list to start checking
86
         * The list name is the called class name e.g. WhiteList in lowercase.
87
         */
88 12
        $list = $this->getSubAccessControlList($listKey);
89
90
        // currently if list key is not set we do not check it
91 12
        if ($list === null || !isset($list[$subListKey])) {
92
            /* @noinspection PhpInconsistentReturnPointsInspection */
93 8
            return;
94
        }
95
96 11
        return in_array($this->getRequest()[$requestKey], $list[$subListKey]);
97
    }
98
99
    /**
100
     * @return mixed
101
     */
102 18
    protected function getShortClassName()
103
    {
104 18
        return $this->getClassUtility()->extractClassNameFromFullName(strtolower(get_called_class()));
105
    }
106
107
    /**
108
     * @return mixed
109
     */
110 19
    public function getRequest()
111
    {
112 19
        return $this->request;
113
    }
114
115
    /**
116
     * @param mixed $request
117
     */
118 22
    public function setRequest($request)
119
    {
120 22
        $this->request = $request;
121 22
    }
122
123
    /**
124
     * @return Dictionary
125
     */
126 18
    public function getDictionary()
127
    {
128 18
        if (!isset($this->dictionary)) {
129 5
            $this->setDictionary(new Dictionary());
130
        }
131
132 18
        return $this->dictionary;
133
    }
134
135
    /**
136
     * @param Dictionary $dictionary
137
     */
138 18
    public function setDictionary(Dictionary $dictionary)
139
    {
140 18
        $this->dictionary = $dictionary;
141 18
    }
142
143
    /**
144
     * @return ApiClient
145
     */
146 11
    public function getApiClient()
147
    {
148 11
        if (!isset($this->apiClient)) {
149 5
            $this->setApiClient(new ApiClient());
150
        }
151
152 11
        return $this->apiClient;
153
    }
154
155
    /**
156
     * @param ApiClient $apiClient
157
     */
158 14
    public function setApiClient(ApiClient $apiClient)
159
    {
160 14
        $this->apiClient = $apiClient;
161 14
    }
162
163
    /**
164
     * @return array|bool
165
     */
166 11
    public function getSlackUserInfo()
167
    {
168
        // get user id in the request
169 11
        $request = $this->getRequest();
170
171
        // currently if user_id is not set we do not check it
172 11
        if (!isset($request['user_id'])) {
173 1
            return false;
174
        }
175
176
        /**
177
         * email normally does not exist in the request.
178
         * Get it by user_id. For this users:read and users:read.email are needed.
179
         */
180 10
        $userInfo = $this->getApiClient()->userInfo(['user' => $request['user_id']]);
181
182 10
        if (empty($userInfo)) {
183
            /*
184
             * Could not find the user in the team
185
             * Probably there might be some issue with Access token and reading user info but block the access
186
             */
187 4
            return false;
188
        }
189
190 6
        return $userInfo;
191
    }
192
193
    /**
194
     * @return ClassUtility
195
     */
196 18
    public function getClassUtility()
197
    {
198 18
        if (!isset($this->classUtility)) {
199 18
            $this->setClassUtility(new ClassUtility());
200
        }
201
202 18
        return $this->classUtility;
203
    }
204
205
    /**
206
     * @param ClassUtility $classUtility
207
     */
208 18
    public function setClassUtility(ClassUtility $classUtility)
209
    {
210 18
        $this->classUtility = $classUtility;
211 18
    }
212
}
213