Passed
Push — master ( 5ec7da...15f37c )
by Ehsan
02:43
created

AbstractAccessList::isEmailInList()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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