loglogins.plugin.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /** @var $modx modX */
  3. $pluginKey = 'loglogins_contexts';
  4. /* Set action based on event name */
  5. if (strpos($modx->event->name, 'Login') !== false) {
  6. $act = $modx->lexicon('login');
  7. } else {
  8. $modx->lexicon->load('core:default');
  9. $act = $modx->lexicon('logout');
  10. }
  11. /* Need this because $modx->user is not available in some cases
  12. and MODX uses it for the actor in logManagerActions()*/
  13. if (isset($user)) {
  14. $modx->user =& $user;
  15. }
  16. $allContexts = array();
  17. /* These are from the vars passed to the event on login.
  18. Converted here to a comma-separated string */
  19. $ac = isset($attributes['addContexts'])
  20. ? $attributes['addContexts']
  21. : array();
  22. $lc = isset($attributes['loginContext'])
  23. ? $attributes['loginContext']
  24. : '';
  25. if ( (empty($ac))) {
  26. $allContexts[] = $lc;
  27. } else {
  28. $allContexts = $ac;
  29. if (! in_array($lc, $ac)) {
  30. $allContexts[] = $lc;
  31. }
  32. }
  33. $msg = !empty($allContexts)? implode(',', $allContexts) : '';
  34. /* $msg is always 'mgr' for manager logins and logouts */
  35. if (strpos($modx->event->name, 'Manager') !== false) {
  36. $msg = 'mgr';
  37. }
  38. switch ($modx->event->name) {
  39. /* Save contexts in user setting */
  40. case 'OnWebLogin':
  41. $setting = $modx->getObject('modUserSetting',
  42. array(
  43. 'user' => $user->get('id'),
  44. 'key' => $pluginKey,
  45. ));
  46. if (! $setting) {
  47. /* Create User Setting if it doesn't exist */
  48. $setting = $modx->newObject('modUserSetting');
  49. $setting->set('user', $user->get('id'));
  50. $setting->set('key', $pluginKey);
  51. $setting->set('namespace', 'loglogins');
  52. }
  53. /* Set value to login contexts */
  54. if ($setting) {
  55. $setting->set('value', $msg);
  56. $setting->save();
  57. }
  58. break;
  59. case 'OnWebLogout':
  60. /* Get contexts from user setting */
  61. $setting = $modx->getObject('modUserSetting',
  62. array(
  63. 'user' => $user->get('id'),
  64. 'key' => $pluginKey,
  65. ));
  66. if ($setting) {
  67. $msg = $setting->get('value');
  68. } else {
  69. $msg = '';
  70. }
  71. break;
  72. }
  73. $modx->logManagerAction($act, 'modContext', $msg);
  74. return '';