1+ <?php
2+
3+ namespace Ominity \Api \Endpoints \Users ;
4+
5+ use Ominity \Api \Endpoints \CollectionEndpointAbstract ;
6+ use Ominity \Api \Exceptions \ApiException ;
7+ use Ominity \Api \Resources \ResourceFactory ;
8+ use Ominity \Api \Resources \Users \User ;
9+ use Ominity \Api \Resources \Users \UserMfaMethod ;
10+ use Ominity \Api \Resources \Users \UserMfaMethodCollection ;
11+
12+ class UserMfaMethodEndpoint extends CollectionEndpointAbstract
13+ {
14+ /**
15+ * @var string
16+ */
17+ protected $ resourcePath = "users/{userId}/mfa-methods " ;
18+
19+ /**
20+ * @inheritDoc
21+ */
22+ protected function getResourceCollectionObject ($ count , $ _links )
23+ {
24+ return new UserMfaMethodCollection ($ this ->client , $ count , $ _links );
25+ }
26+
27+ /**
28+ * @inheritDoc
29+ */
30+ protected function getResourceObject ()
31+ {
32+ return new UserMfaMethod ($ this ->client );
33+ }
34+
35+ /**
36+ * Enable a specific mfa method for a specific User.
37+ *
38+ * @param User $user
39+ * @param string $method
40+ * @param array $data
41+ * @param array $filters
42+ *
43+ * @return UserMfaMethodCollection
44+ * @throws \Ominity\Api\Exceptions\ApiException
45+ */
46+ public function enableFor (User $ user , string $ method , array $ data = [], array $ filters = [])
47+ {
48+ return $ this ->enableForId ($ user ->id , $ method , $ data , $ filters );
49+ }
50+
51+ /**
52+ * Enable a specific mfa method for a specific User ID.
53+ *
54+ * @param int $userId
55+ * @param array $data
56+ * @param array $filters
57+ *
58+ * @return UserMfaMethodCollection
59+ * @throws \Ominity\Api\Exceptions\ApiException
60+ */
61+ public function enableForId ($ userId , string $ method , array $ data = [], array $ filters = [])
62+ {
63+ $ this ->setPathVariables (['userId ' => $ userId ]);
64+
65+ $ result = $ this ->client ->performHttpCall (
66+ self ::REST_CREATE ,
67+ $ this ->getResourcePath () . '/ ' . $ method . $ this ->buildQueryString ($ filters ),
68+ $ this ->parseRequestBody ($ data )
69+ );
70+
71+ return ResourceFactory::createFromApiResult ($ result , $ this ->getResourceObject ());
72+ }
73+
74+ /**
75+ * Get the mfa method for a specific User.
76+ *
77+ * @param User $user
78+ * @param string $method
79+ * @return UserMfaMethod
80+ *
81+ * @throws \Ominity\Api\Exceptions\ApiException
82+ */
83+ public function getFor (User $ user , string $ method , array $ parameters = []) {
84+ if (empty ($ user )) {
85+ throw new ApiException ("User is empty. " );
86+ }
87+
88+ if (empty ($ method )) {
89+ throw new ApiException ("Method is empty. " );
90+ }
91+
92+ return $ this ->getForId ($ user ->id , $ method , $ parameters );
93+ }
94+
95+ /**
96+ * Get the mfa method for a specific User ID.
97+ *
98+ * @param int $userId
99+ * @param string $method
100+ * @return UserMfaMethod
101+ *
102+ * @throws \Ominity\Api\Exceptions\ApiException
103+ */
104+ public function getForId (int $ userId , string $ method , array $ parameters = []) {
105+ if (empty ($ userId )) {
106+ throw new ApiException ("User ID is empty. " );
107+ }
108+
109+ if (empty ($ method )) {
110+ throw new ApiException ("Method is empty. " );
111+ }
112+
113+ $ this ->setPathVariables (['userId ' => $ userId ]);
114+ return parent ::rest_read ($ method , $ parameters );
115+ }
116+
117+ /**
118+ * List the mfa methods for a specific User.
119+ *
120+ * @param User $user
121+ * @param array $parameters
122+ * @return UserMfaMethodCollection
123+ *
124+ * @throws \Ominity\Api\Exceptions\ApiException
125+ */
126+ public function listFor (User $ user , array $ parameters = [])
127+ {
128+ return $ this ->listForId ($ user ->id , $ parameters );
129+ }
130+
131+ /**
132+ * List the mfa methods for a specific User ID.
133+ *
134+ * @param int $userId
135+ * @param array $parameters
136+ * @return UserMfaMethodCollection
137+ *
138+ * @throws \Ominity\Api\Exceptions\ApiException
139+ */
140+ public function listForId (int $ userId , array $ parameters = [])
141+ {
142+ $ this ->setPathVariables (['userId ' => $ userId ]);
143+
144+ return parent ::rest_list (null , null , $ parameters );
145+ }
146+
147+ /**
148+ * Validate MFA code for a specific User.
149+ *
150+ * @param User $user
151+ * @param string $method
152+ * @param array $data
153+ * @param array $filters
154+ *
155+ * @return bool
156+ * @throws \Ominity\Api\Exceptions\ApiException
157+ */
158+ public function validateFor (User $ user , string $ method , array $ data , array $ filters = [])
159+ {
160+ return $ this ->validateForId ($ user ->id , $ method , $ data , $ filters );
161+ }
162+
163+ /**
164+ * Validate MFA code for a specific User ID.
165+ *
166+ * @param int $userId
167+ * @param string $method
168+ * @param array $data
169+ * @param array $filters
170+ *
171+ * @return bool
172+ * @throws \Ominity\Api\Exceptions\ApiException
173+ */
174+ public function validateForId ($ userId , string $ method , array $ data , array $ filters = [])
175+ {
176+ $ this ->setPathVariables (['userId ' => $ userId ]);
177+
178+ $ result = $ this ->client ->performHttpCall (
179+ self ::REST_CREATE ,
180+ $ this ->getResourcePath () . '/ ' . $ method . '/validate ' . $ this ->buildQueryString ($ filters ),
181+ $ this ->parseRequestBody ($ data )
182+ );
183+
184+ return $ result ->success ?? false ;
185+ }
186+
187+ /**
188+ * Disable the given mfa method for a specific User.
189+ *
190+ * Will throw a ApiException if the method is invalid or the resource cannot be found.
191+ * Returns with HTTP status No Content (204) if successful.
192+ *
193+ * @param User $user
194+ * @param string $method
195+ * @param array $data
196+ * @return UserMfaMethod
197+ * @throws ApiException
198+ */
199+ public function disableFor (User $ user , string $ method , array $ data = [])
200+ {
201+ return $ this ->disableForId ($ user ->id , $ method , $ data );
202+ }
203+
204+ /**
205+ * Disables the given mfa method for a specific User ID.
206+ *
207+ * Will throw a ApiException if the method is invalid or the resource cannot be found.
208+ * Returns with HTTP status No Content (204) if successful.
209+ *
210+ * @param int $userId
211+ * @param string $method
212+ * @param array $data
213+ * @return UserMfaMethod
214+ * @throws ApiException
215+ */
216+ public function disableForId (int $ userId , string $ method , array $ data = [])
217+ {
218+ $ this ->setPathVariables (['userId ' => $ userId ]);
219+
220+ return $ this ->rest_delete ($ method , $ data );
221+ }
222+
223+ /**
224+ * Send MFA code for a specific User.
225+ *
226+ * @param User $user
227+ * @param string $method
228+ * @param array $data
229+ * @param array $filters
230+ *
231+ * @return bool
232+ * @throws \Ominity\Api\Exceptions\ApiException
233+ */
234+ public function sendFor (User $ user , string $ method , array $ data , array $ filters = [])
235+ {
236+ return $ this ->validateForId ($ user ->id , $ method , $ data , $ filters );
237+ }
238+
239+ /**
240+ * Send MFA code for a specific User ID.
241+ *
242+ * @param int $userId
243+ * @param string $method
244+ * @param array $data
245+ * @param array $filters
246+ *
247+ * @return bool
248+ * @throws \Ominity\Api\Exceptions\ApiException
249+ */
250+ public function sendForId ($ userId , string $ method , array $ data , array $ filters = [])
251+ {
252+ $ this ->setPathVariables (['userId ' => $ userId ]);
253+
254+ $ result = $ this ->client ->performHttpCall (
255+ self ::REST_CREATE ,
256+ $ this ->getResourcePath () . '/ ' . $ method . '/send ' . $ this ->buildQueryString ($ filters ),
257+ $ this ->parseRequestBody ($ data )
258+ );
259+
260+ return $ result ->success ?? false ;
261+ }
262+ }
0 commit comments