PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/dashboard/api/auth/key.php

https://bitbucket.org/concrete5api/api
PHP | 102 lines | 92 code | 10 blank | 0 comment | 18 complexity | 3fc2925e9b0ef63f0947c5e93d5d9ab3 MD5 | raw file
  1. <?php defined('C5_EXECUTE') or die('Access Denied');
  2. class DashboardApiAuthKeyController extends DashboardBaseController {
  3. public function view($updated = false, $id = false) {
  4. if($updated) {
  5. switch ($updated) {
  6. case 'new':
  7. $obj = ApiAuthKeyModel::getByAppID($id);
  8. $succ = t('New Key Generated.')."\n";
  9. $succ .= t('App ID: %s', $obj->appID)."\n";
  10. $succ .= t('Public Key: %s', $obj->publicKey)."\n";
  11. $succ .= t('Private Key: %s', $obj->privateKey)."\n";
  12. $this->set('success', $succ);
  13. break;
  14. case 'deleted':
  15. $this->set('success', t('Key Successfully Deleted.'));
  16. break;
  17. case 'disabled':
  18. $this->set('success', t('Key Successfully Disabled.'));
  19. break;
  20. case 'enabled':
  21. $this->set('success', t('Key Successfully Enabled.'));
  22. break;
  23. case 'invalid_key':
  24. $this->set('error', t('Invalid Key!'));
  25. break;
  26. case 'invalid_token':
  27. $this->set('error', Loader::helper('validation/token')->getErrorMessage());
  28. break;
  29. }
  30. }
  31. $list = new ApiAuthKeyList();
  32. $list = $list->get();
  33. $this->set('list', $list);
  34. }
  35. public function delete($key = false, $token = false) {
  36. if(!$key || !$token) { //if this happens they someone is trying to hack it so no error message for them.
  37. $this->redirect('/dashboard/api/auth/key');
  38. }
  39. $obj = ApiAuthKeyModel::getByAppID($key);
  40. if(!is_object($obj) || !$obj->appID) {
  41. $this->redirect('/dashboard/api/auth/key', 'invalid_key');
  42. }
  43. $valt = Loader::helper('validation/token');
  44. if(!$valt->validate('delete', $token)) {
  45. $this->redirect('/dashboard/api/auth/key', 'invalid_token');
  46. }
  47. $obj->delete();
  48. $this->redirect('/dashboard/api/auth/key', 'deleted');
  49. }
  50. public function generate($token = false) {
  51. $valt = Loader::helper('validation/token');
  52. if(!$valt->validate('generate', $token)) {
  53. $this->redirect('/dashboard/api/auth/key', 'invalid_token');
  54. }
  55. $obj = ApiAuthKeyModel::add();
  56. $id = $obj->appID;
  57. $this->redirect('/dashboard/api/auth/key', 'new', $id);
  58. }
  59. public function disable($key = false, $token = false) {
  60. if(!$key || !$token) { //if this happens they someone is trying to hack it so no error message for them.
  61. $this->redirect('/dashboard/api/auth/key');
  62. }
  63. $obj = ApiAuthKeyModel::getByAppID($key);
  64. if(!is_object($obj) || !$obj->appID) {
  65. $this->redirect('/dashboard/api/auth/key', 'invalid_key');
  66. }
  67. $valt = Loader::helper('validation/token');
  68. if(!$valt->validate('disable', $token)) {
  69. $this->redirect('/dashboard/api/auth/key', 'invalid_token');
  70. }
  71. $db = Loader::db();
  72. $obj->active = 0;
  73. $obj->save();
  74. $this->redirect('/dashboard/api/auth/key', 'disabled');
  75. }
  76. public function enable($key = false, $token = false) {
  77. if(!$key || !$token) { //if this happens they someone is trying to hack it so no error message for them.
  78. $this->redirect('/dashboard/api/auth/key');
  79. }
  80. $obj = ApiAuthKeyModel::getByAppID($key);
  81. if(!is_object($obj) || !$obj->appID) {
  82. $this->redirect('/dashboard/api/auth/key', 'invalid_key');
  83. }
  84. $valt = Loader::helper('validation/token');
  85. if(!$valt->validate('enable', $token)) {
  86. $this->redirect('/dashboard/api/auth/key', 'invalid_token');
  87. }
  88. $obj->active = 1;
  89. $obj->save();
  90. $this->redirect('/dashboard/api/auth/key', 'enabled');
  91. }
  92. }