PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/core/Auth/Controller/Accounts.php

https://github.com/scogle/cockpit
PHP | 237 lines | 148 code | 88 blank | 1 comment | 27 complexity | 0b186d4c3a9b31442eae7fc94edaa506 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. namespace Auth\Controller;
  3. class Accounts extends \Cockpit\Controller {
  4. public function index() {
  5. $current = $this->user["_id"];
  6. $accounts = $this->app->db->find("cockpit/accounts", [
  7. "filter" => $this->user["group"]=="admin" ? null:["_id"=>$current],
  8. "sort" => ["user" => 1]
  9. ])->toArray();
  10. foreach ($accounts as &$account) {
  11. $account["md5email"] = md5(@$account["email"]);
  12. }
  13. return $this->render('auth:views/accounts/index.php', compact('accounts', 'current'));
  14. }
  15. public function account($uid=null) {
  16. if(!$uid) {
  17. $uid = $this->user["_id"];
  18. }
  19. $account = $this->app->db->findOne("cockpit/accounts", ["_id" => $uid]);
  20. if(!$account) {
  21. return false;
  22. }
  23. unset($account["password"]);
  24. $languages = $this->getLanguages();
  25. $groups = $this->getGroups();
  26. return $this->render('auth:views/accounts/account.php', compact('account', 'uid', 'languages', 'groups'));
  27. }
  28. public function create() {
  29. $uid = null;
  30. $account = ["user"=>"", "email"=>"", "active"=>1, "group"=>"admin", "i18n"=>$this->app->helper("i18n")->locale];
  31. $languages = $this->getLanguages();
  32. $groups = $this->getGroups();
  33. return $this->render('auth:views/accounts/account.php', compact('account', 'uid', 'languages', 'groups'));
  34. }
  35. public function save() {
  36. if($data = $this->param("account", false)) {
  37. if(isset($data["password"])) {
  38. if(strlen($data["password"])){
  39. $data["password"] = $this->app->hash($data["password"]);
  40. } else {
  41. unset($data["password"]);
  42. }
  43. }
  44. $this->app->db->save("cockpit/accounts", $data);
  45. if(isset($data["password"])) {
  46. unset($data["password"]);
  47. }
  48. if($data["_id"] == $this->user["_id"]) {
  49. $this->module("auth")->setUser($data);
  50. }
  51. return json_encode($data);
  52. }
  53. return false;
  54. }
  55. public function remove() {
  56. if($data = $this->param("account", false)) {
  57. // user can't delete himself
  58. if($data["_id"] != $this->user["_id"]) {
  59. $this->app->db->remove("cockpit/accounts", ["_id" => $data["_id"]]);
  60. return '{"success":true}';
  61. }
  62. }
  63. return false;
  64. }
  65. public function groups() {
  66. if($this->user["group"]!="admin") return false;
  67. $acl = $this->getAcl();
  68. return $this->render('auth:views/accounts/groups.php', compact('acl'));
  69. }
  70. public function addOrEditGroup() {
  71. if($this->user["group"]!="admin") return false;
  72. if($name = $this->app->param("name", false)) {
  73. if($name!="admin") {
  74. $groups = $this->app->memory->get("cockpit.acl.groups", []);
  75. if($oldname = $this->app->param("oldname", false)) {
  76. if(isset($groups[$oldname]) && $oldname!="admin") {
  77. $rights = $this->app->memory->get("cockpit.acl.rights", []);
  78. if(isset($rights[$oldname])) {
  79. $rights[$name] = $rights[$oldname];
  80. unset($rights[$oldname]);
  81. $this->app->memory->set("cockpit.acl.rights", $rights);
  82. }
  83. $this->app->db->update("cockpit/accounts", ["group"=>$oldname], ["group"=>$name]);
  84. unset($groups[$oldname]);
  85. }
  86. }
  87. $groups[$name] = false;
  88. $this->app->memory->set("cockpit.acl.groups", $groups);
  89. }
  90. }
  91. $acl = $this->getAcl();
  92. return json_encode($acl);
  93. }
  94. public function deleteGroup() {
  95. if($this->user["group"]!="admin") return false;
  96. if($name = $this->app->param("name", false)) {
  97. if($name!="admin") {
  98. $groups = $this->app->memory->get("cockpit.acl.groups", []);
  99. if(isset($groups[$name])) {
  100. unset($groups[$name]);
  101. $this->app->db->update("cockpit/accounts", ["group"=>""], ["group"=>$name]);
  102. }
  103. $this->app->memory->set("cockpit.acl.groups", $groups);
  104. }
  105. }
  106. $acl = $this->getAcl();
  107. return json_encode($acl);
  108. }
  109. public function saveAcl() {
  110. if($this->user["group"]!="admin") return false;
  111. if($acl = $this->app->param("acl", false)) {
  112. $this->app->memory->set("cockpit.acl.rights", $acl);
  113. }
  114. if($settings = $this->app->param("aclSettings", false)) {
  115. $this->app->memory->set("cockpit.acl.groups.settings", $settings);
  116. }
  117. return '{"success":true}';
  118. }
  119. protected function getLanguages() {
  120. $languages = [];
  121. foreach ($this->app->helper("fs")->ls('*.php', 'cockpit:i18n') as $file) {
  122. $lang = include($file->getRealPath());
  123. $i18n = $file->getBasename('.php');
  124. $language = isset($lang['@meta']['language']) ? $lang['@meta']['language'] : $i18n;
  125. $languages[] = ["i18n" => $i18n, "language"=> $language];
  126. }
  127. return $languages;
  128. }
  129. protected function getGroups() {
  130. $groups = ['admin'];
  131. foreach ($this->app->memory->get("cockpit.acl.groups", []) as $group => $isadmin) {
  132. $groups[] = $group;
  133. }
  134. return $groups;
  135. }
  136. protected function getAcl() {
  137. $acl = [];
  138. foreach ($this->app->helper("acl")->getGroups() as $group => $isadmin) {
  139. $acl[$group] = new \ArrayObject([]);
  140. foreach ($this->app->helper("acl")->getResources() as $resource => $actions) {
  141. $acl[$group][$resource] = new \ArrayObject([]);
  142. foreach ($actions as $action) {
  143. $acl[$group][$resource][$action] = $this->app->helper("acl")->hasaccess($group, $resource, $action);
  144. }
  145. }
  146. }
  147. return $acl;
  148. }
  149. }