PageRenderTime 18ms CodeModel.GetById 1ms app.highlight 13ms RepoModel.GetById 1ms 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
  1<?php
  2
  3namespace Auth\Controller;
  4
  5class Accounts extends \Cockpit\Controller {
  6
  7    public function index() {
  8
  9        $current  = $this->user["_id"];
 10        $accounts = $this->app->db->find("cockpit/accounts", [
 11            "filter" => $this->user["group"]=="admin" ? null:["_id"=>$current],
 12            "sort"   => ["user" => 1]
 13        ])->toArray();
 14
 15        foreach ($accounts as &$account) {
 16            $account["md5email"] = md5(@$account["email"]);
 17        }
 18
 19        return $this->render('auth:views/accounts/index.php', compact('accounts', 'current'));
 20    }
 21
 22
 23    public function account($uid=null) {
 24
 25        if(!$uid) {
 26            $uid = $this->user["_id"];
 27        }
 28
 29        $account = $this->app->db->findOne("cockpit/accounts", ["_id" => $uid]);
 30
 31        if(!$account) {
 32            return false;
 33        }
 34
 35        unset($account["password"]);
 36
 37        $languages = $this->getLanguages();
 38        $groups    = $this->getGroups();
 39
 40        return $this->render('auth:views/accounts/account.php', compact('account', 'uid', 'languages', 'groups'));
 41    }
 42
 43    public function create() {
 44
 45        $uid     = null;
 46        $account = ["user"=>"", "email"=>"", "active"=>1, "group"=>"admin", "i18n"=>$this->app->helper("i18n")->locale];
 47
 48        $languages = $this->getLanguages();
 49        $groups    = $this->getGroups();
 50
 51        return $this->render('auth:views/accounts/account.php', compact('account', 'uid', 'languages', 'groups'));
 52    }
 53
 54    public function save() {
 55
 56        if($data = $this->param("account", false)) {
 57
 58
 59            if(isset($data["password"])) {
 60                if(strlen($data["password"])){
 61                    $data["password"] = $this->app->hash($data["password"]);
 62                } else {
 63                    unset($data["password"]);
 64                }
 65            }
 66
 67            $this->app->db->save("cockpit/accounts", $data);
 68
 69            if(isset($data["password"])) {
 70                unset($data["password"]);
 71            }
 72
 73            if($data["_id"] == $this->user["_id"]) {
 74
 75                $this->module("auth")->setUser($data);
 76            }
 77
 78            return json_encode($data);
 79        }
 80
 81        return false;
 82
 83    }
 84
 85    public function remove() {
 86
 87        if($data = $this->param("account", false)) {
 88
 89            // user can't delete himself
 90            if($data["_id"] != $this->user["_id"]) {
 91
 92                $this->app->db->remove("cockpit/accounts", ["_id" => $data["_id"]]);
 93
 94                return '{"success":true}';
 95            }
 96        }
 97
 98        return false;
 99    }
100
101    public function groups() {
102
103        if($this->user["group"]!="admin") return false;
104
105        $acl = $this->getAcl();
106
107        return $this->render('auth:views/accounts/groups.php', compact('acl'));
108    }
109
110
111    public function addOrEditGroup() {
112
113        if($this->user["group"]!="admin") return false;
114
115        if($name = $this->app->param("name", false)) {
116
117            if($name!="admin") {
118                $groups = $this->app->memory->get("cockpit.acl.groups", []);
119
120
121                if($oldname = $this->app->param("oldname", false)) {
122
123                    if(isset($groups[$oldname]) && $oldname!="admin") {
124
125                        $rights = $this->app->memory->get("cockpit.acl.rights", []);
126
127                        if(isset($rights[$oldname])) {
128                            $rights[$name] = $rights[$oldname];
129                            unset($rights[$oldname]);
130                            $this->app->memory->set("cockpit.acl.rights", $rights);
131                        }
132
133                        $this->app->db->update("cockpit/accounts", ["group"=>$oldname], ["group"=>$name]);
134
135                        unset($groups[$oldname]);
136                    }
137
138                }
139
140                $groups[$name] = false;
141
142                $this->app->memory->set("cockpit.acl.groups", $groups);
143            }
144        }
145
146        $acl = $this->getAcl();
147
148        return json_encode($acl);
149    }
150
151    public function deleteGroup() {
152
153        if($this->user["group"]!="admin") return false;
154
155        if($name = $this->app->param("name", false)) {
156
157            if($name!="admin") {
158                $groups = $this->app->memory->get("cockpit.acl.groups", []);
159
160                if(isset($groups[$name])) {
161                    unset($groups[$name]);
162                    $this->app->db->update("cockpit/accounts", ["group"=>""], ["group"=>$name]);
163                }
164
165                $this->app->memory->set("cockpit.acl.groups", $groups);
166            }
167        }
168
169        $acl = $this->getAcl();
170
171        return json_encode($acl);
172    }
173
174    public function saveAcl() {
175
176        if($this->user["group"]!="admin") return false;
177
178        if($acl = $this->app->param("acl", false)) {
179            $this->app->memory->set("cockpit.acl.rights", $acl);
180        }
181
182        if($settings = $this->app->param("aclSettings", false)) {
183            $this->app->memory->set("cockpit.acl.groups.settings", $settings);
184        }
185
186        return '{"success":true}';
187    }
188
189    protected function getLanguages() {
190
191        $languages = [];
192
193        foreach ($this->app->helper("fs")->ls('*.php', 'cockpit:i18n') as $file) {
194
195            $lang = include($file->getRealPath());
196            $i18n = $file->getBasename('.php');
197            $language = isset($lang['@meta']['language']) ? $lang['@meta']['language'] : $i18n;
198
199            $languages[] = ["i18n" => $i18n, "language"=> $language];
200
201        }
202
203        return $languages;
204    }
205
206    protected function getGroups() {
207
208        $groups = ['admin'];
209
210        foreach ($this->app->memory->get("cockpit.acl.groups", []) as $group => $isadmin) {
211            $groups[] = $group;
212        }
213
214        return $groups;
215    }
216
217    protected function getAcl() {
218
219        $acl = [];
220
221        foreach ($this->app->helper("acl")->getGroups() as $group => $isadmin) {
222
223            $acl[$group] = new \ArrayObject([]);
224
225            foreach ($this->app->helper("acl")->getResources() as $resource => $actions) {
226                $acl[$group][$resource] = new \ArrayObject([]);
227
228                foreach ($actions as $action) {
229                    $acl[$group][$resource][$action] = $this->app->helper("acl")->hasaccess($group, $resource, $action);
230                }
231            }
232        }
233
234        return $acl;
235    }
236
237}