CMS  Version 3.9
APIManager Class Reference

Public Member Functions

 ApiManager ()
 
 validateAPILogin ()
 
 extendToken ()
 

Static Public Member Functions

static registerEndPoint ($endpoint, $handler)
 
static getEndPoint ($endpoint)
 
static dispatch ($endpoint)
 
static registerAPIEndPoints ()
 
static setDefaults ()
 
static registerSectionContentManager ()
 
static upgradeComponent ($version)
 

Detailed Description

Definition at line 164 of file api_manager.inc.

Member Function Documentation

◆ ApiManager()

APIManager::ApiManager ( )

Definition at line 168 of file api_manager.inc.

169  {
170 
171  }

◆ dispatch()

static APIManager::dispatch (   $endpoint)
static

Definition at line 189 of file api_manager.inc.

190  {
191  global $method;
192  global $user;
193 
194  $handler = APIManager::getEndPoint($endpoint);
195  if (!$handler)
196  {
197  throw new FakoliException("API Endpoint not found");
198  }
199 
200  $m = strtolower($method);
201  if (!method_exists($handler, $m))
202  {
203  throw new FakoliException("Unsupported HTTP Verb");
204  }
205 
206  if ($handler->allowAnonymous())
207  {
208  $handler->$m();
209  return;
210  }
211 
212  $t = checkIdentifier($_GET["token"]);
213 
214  if (!$t)
215  {
216  throw new FakoliException("No Authentication Token Provided");
217  }
218 
220  if (!$token)
221  {
222  throw new FakoliException("Authentication Token Not Found");
223  }
224 
225  if (!$token->active)
226  {
227  throw new FakoliException("Invalid Token");
228  }
229 
230  $user = $token->User();
231 
232  if (!$handler->checkAccess())
233  {
234  throw new FakoliException("Permission Denied");
235  }
236 
237  $handler->$m();
238  }
$handler
Definition: event_form.inc:62
static getEndPoint($endpoint)
static getToken($token)
Definition: api_token.inc:49
FakoliException is the base exception class for all Fakoli errors.
Definition: core.inc:53
global $user
$method
Pull out a simple reference to the request method.
Definition: core.inc:1573

◆ extendToken()

APIManager::extendToken ( )

Definition at line 304 of file api_manager.inc.

305  {
306  $t = checkIdentifier($_GET["token"]);
307 
309 
310  $tokenLifetime = Settings::getValue("api", "token_lifetime");
311  if ($tokenLifetime)
312  {
313  $expiryDate = new DateTime();
314  $expiryDate->modify("+".$tokenLifetime);
315  $token->expiry_date = $expiryDate->format("Y-m-d H:i:s T");
316  $token->save();
317  }
318  }
static getValue($component, $name)
Retrieve the value of the specified Setting.
Definition: settings.inc:104

◆ getEndPoint()

static APIManager::getEndPoint (   $endpoint)
static

Definition at line 178 of file api_manager.inc.

179  {
180  if (!APIManager::$endpoints)
181  {
182  APIManager::$endpoints = array();
183  ComponentManager::fireEvent('RegisterAPIEndPoints');
184  }
185 
186  return APIManager::$endpoints[$endpoint];
187  }
static fireEvent($event, $parameter=null, $mustBeConsumed=false)
Fire an event to all subscribers as detailed in their manifests.

◆ registerAPIEndPoints()

static APIManager::registerAPIEndPoints ( )
static

Definition at line 320 of file api_manager.inc.

321  {
324  }
static registerEndPoint($endpoint, $handler)

◆ registerEndPoint()

static APIManager::registerEndPoint (   $endpoint,
  $handler 
)
static

Definition at line 173 of file api_manager.inc.

174  {
175  APIManager::$endpoints[$endpoint] = $handler;
176  }

◆ registerSectionContentManager()

static APIManager::registerSectionContentManager ( )
static

Definition at line 331 of file api_manager.inc.

332  {
334  }
Provides a central management class for event handlers and common functionality for the api component...
Definition: api_manager.inc:98
static registerManager($type, $manager)
Registers a SectionContentManager for handling a specified section type.

◆ setDefaults()

static APIManager::setDefaults ( )
static

Definition at line 326 of file api_manager.inc.

327  {
328  Settings::setDefaultValue("api", "token_lifetime", "30 days", "String", "Specifies how long API authorization tokens stay valid. Leave blank to have tokens never expire");
329  }
static setDefaultValue($component, $name, $value, $field_type="String", $annotation="", $category="", $options="", $weight=0)
Sets the default value of the given component setting.
Definition: settings.inc:174

◆ upgradeComponent()

static APIManager::upgradeComponent (   $version)
static

Definition at line 336 of file api_manager.inc.

337  {
338  $mgr = new APIUpgradeManager();
339  $mgr->upgrade($version);
340  }

◆ validateAPILogin()

APIManager::validateAPILogin ( )

Definition at line 240 of file api_manager.inc.

241  {
242  global $user;
243 
244  $mgr = new UserManager();
245 
246  $username = $_REQUEST[$mgr->getUsernameField()];
247  $password = $_REQUEST["password"];
248 
249  if (Settings::getValue("login", "reject_all_blank_passwords"))
250  {
251  // JDG - guard against data not found exception
252  if(!$username OR !$password)
253  {
254  LoginManager::recordLoginAttempt($username, "api", null, "failure");
255  LoginManager::$error = "Incorrect user name or password.";
256  return "ERROR|".LoginManager::$error;
257  }
258  }
259  else
260  {
261  if(!$username)
262  {
263  LoginManager::recordLoginAttempt($username, "api", null, "failure");
264  LoginManager::$error = "Incorrect user name or password.";
265  return "ERROR|".LoginManager::$error;
266  }
267  }
268 
269  ComponentManager::fireEvent("OnLoginSubmit");
270 
271  $user = $mgr->validatePassword($username, $password);
272  if ($user)
273  {
274  $token = APIToken::getUserToken($user->user_id);
275  if (!$token)
276  {
277  $token = new APIToken();
278  $token->token = plainGUID();
279  $token->user_id = $user->user_id;
280  $token->created_date = now();
281  $token->expiry_date = null;
282  $token->active = true;
283  $token->last_access = now();
284  $token->save();
285  }
286 
287  $tokenLifetime = Settings::getValue("api", "token_lifetime");
288  if ($tokenLifetime)
289  {
290  $expiryDate = new DateTime();
291  $expiryDate->modify("+".$tokenLifetime);
292  $token->expiry_date = $expiryDate->format("Y-m-d H:i:s T");
293  }
294 
295  LoginManager::recordLoginAttempt($username, "api", null, "success");
296  Fakoli::JSONReturn($token, true);
297  }
298  else
299  {
300  return "ERROR";
301  }
302  }
$username
static getUserToken($user_id)
Definition: api_token.inc:39
static recordLoginAttempt($username, $mode, $token, $result)
Records a login attempt in the login audit trail.
Definition: login.inc:117
static $error
Definition: login.inc:96
Provides the interface to the user model for the application.

The documentation for this class was generated from the following file: