├── .gitignore
├── LICENSE
├── README.md
├── config
└── ion_auth_acl.php
├── controllers
├── Admin.php
├── Auth.php
└── Dashboard.php
├── language
└── english
│ └── ion_auth_acl_lang.php
├── libraries
└── Ion_auth_acl.php
├── models
└── ion_auth_acl_model.php
├── sql
└── install.sql
└── views
├── admin
├── add_permission.php
├── dashboard.php
├── delete_permission.php
├── edit_permission.php
├── group_permissions.php
├── groups.php
├── manage.php
├── manage_user.php
├── permissions.php
├── user_permissions.php
└── users.php
└── auth
└── login.php
/.gitignore:
--------------------------------------------------------------------------------
1 | */config/development
2 | */logs/log-*.php
3 | */logs/!index.html
4 | */cache/*
5 | */cache/!index.html
6 | */cache/!.htaccess
7 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 2, June 1991
3 |
4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | Preamble
10 |
11 | The licenses for most software are designed to take away your
12 | freedom to share and change it. By contrast, the GNU General Public
13 | License is intended to guarantee your freedom to share and change free
14 | software--to make sure the software is free for all its users. This
15 | General Public License applies to most of the Free Software
16 | Foundation's software and to any other program whose authors commit to
17 | using it. (Some other Free Software Foundation software is covered by
18 | the GNU Lesser General Public License instead.) You can apply it to
19 | your programs, too.
20 |
21 | When we speak of free software, we are referring to freedom, not
22 | price. Our General Public Licenses are designed to make sure that you
23 | have the freedom to distribute copies of free software (and charge for
24 | this service if you wish), that you receive source code or can get it
25 | if you want it, that you can change the software or use pieces of it
26 | in new free programs; and that you know you can do these things.
27 |
28 | To protect your rights, we need to make restrictions that forbid
29 | anyone to deny you these rights or to ask you to surrender the rights.
30 | These restrictions translate to certain responsibilities for you if you
31 | distribute copies of the software, or if you modify it.
32 |
33 | For example, if you distribute copies of such a program, whether
34 | gratis or for a fee, you must give the recipients all the rights that
35 | you have. You must make sure that they, too, receive or can get the
36 | source code. And you must show them these terms so they know their
37 | rights.
38 |
39 | We protect your rights with two steps: (1) copyright the software, and
40 | (2) offer you this license which gives you legal permission to copy,
41 | distribute and/or modify the software.
42 |
43 | Also, for each author's protection and ours, we want to make certain
44 | that everyone understands that there is no warranty for this free
45 | software. If the software is modified by someone else and passed on, we
46 | want its recipients to know that what they have is not the original, so
47 | that any problems introduced by others will not reflect on the original
48 | authors' reputations.
49 |
50 | Finally, any free program is threatened constantly by software
51 | patents. We wish to avoid the danger that redistributors of a free
52 | program will individually obtain patent licenses, in effect making the
53 | program proprietary. To prevent this, we have made it clear that any
54 | patent must be licensed for everyone's free use or not licensed at all.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | GNU GENERAL PUBLIC LICENSE
60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61 |
62 | 0. This License applies to any program or other work which contains
63 | a notice placed by the copyright holder saying it may be distributed
64 | under the terms of this General Public License. The "Program", below,
65 | refers to any such program or work, and a "work based on the Program"
66 | means either the Program or any derivative work under copyright law:
67 | that is to say, a work containing the Program or a portion of it,
68 | either verbatim or with modifications and/or translated into another
69 | language. (Hereinafter, translation is included without limitation in
70 | the term "modification".) Each licensee is addressed as "you".
71 |
72 | Activities other than copying, distribution and modification are not
73 | covered by this License; they are outside its scope. The act of
74 | running the Program is not restricted, and the output from the Program
75 | is covered only if its contents constitute a work based on the
76 | Program (independent of having been made by running the Program).
77 | Whether that is true depends on what the Program does.
78 |
79 | 1. You may copy and distribute verbatim copies of the Program's
80 | source code as you receive it, in any medium, provided that you
81 | conspicuously and appropriately publish on each copy an appropriate
82 | copyright notice and disclaimer of warranty; keep intact all the
83 | notices that refer to this License and to the absence of any warranty;
84 | and give any other recipients of the Program a copy of this License
85 | along with the Program.
86 |
87 | You may charge a fee for the physical act of transferring a copy, and
88 | you may at your option offer warranty protection in exchange for a fee.
89 |
90 | 2. You may modify your copy or copies of the Program or any portion
91 | of it, thus forming a work based on the Program, and copy and
92 | distribute such modifications or work under the terms of Section 1
93 | above, provided that you also meet all of these conditions:
94 |
95 | a) You must cause the modified files to carry prominent notices
96 | stating that you changed the files and the date of any change.
97 |
98 | b) You must cause any work that you distribute or publish, that in
99 | whole or in part contains or is derived from the Program or any
100 | part thereof, to be licensed as a whole at no charge to all third
101 | parties under the terms of this License.
102 |
103 | c) If the modified program normally reads commands interactively
104 | when run, you must cause it, when started running for such
105 | interactive use in the most ordinary way, to print or display an
106 | announcement including an appropriate copyright notice and a
107 | notice that there is no warranty (or else, saying that you provide
108 | a warranty) and that users may redistribute the program under
109 | these conditions, and telling the user how to view a copy of this
110 | License. (Exception: if the Program itself is interactive but
111 | does not normally print such an announcement, your work based on
112 | the Program is not required to print an announcement.)
113 |
114 | These requirements apply to the modified work as a whole. If
115 | identifiable sections of that work are not derived from the Program,
116 | and can be reasonably considered independent and separate works in
117 | themselves, then this License, and its terms, do not apply to those
118 | sections when you distribute them as separate works. But when you
119 | distribute the same sections as part of a whole which is a work based
120 | on the Program, the distribution of the whole must be on the terms of
121 | this License, whose permissions for other licensees extend to the
122 | entire whole, and thus to each and every part regardless of who wrote it.
123 |
124 | Thus, it is not the intent of this section to claim rights or contest
125 | your rights to work written entirely by you; rather, the intent is to
126 | exercise the right to control the distribution of derivative or
127 | collective works based on the Program.
128 |
129 | In addition, mere aggregation of another work not based on the Program
130 | with the Program (or with a work based on the Program) on a volume of
131 | a storage or distribution medium does not bring the other work under
132 | the scope of this License.
133 |
134 | 3. You may copy and distribute the Program (or a work based on it,
135 | under Section 2) in object code or executable form under the terms of
136 | Sections 1 and 2 above provided that you also do one of the following:
137 |
138 | a) Accompany it with the complete corresponding machine-readable
139 | source code, which must be distributed under the terms of Sections
140 | 1 and 2 above on a medium customarily used for software interchange; or,
141 |
142 | b) Accompany it with a written offer, valid for at least three
143 | years, to give any third party, for a charge no more than your
144 | cost of physically performing source distribution, a complete
145 | machine-readable copy of the corresponding source code, to be
146 | distributed under the terms of Sections 1 and 2 above on a medium
147 | customarily used for software interchange; or,
148 |
149 | c) Accompany it with the information you received as to the offer
150 | to distribute corresponding source code. (This alternative is
151 | allowed only for noncommercial distribution and only if you
152 | received the program in object code or executable form with such
153 | an offer, in accord with Subsection b above.)
154 |
155 | The source code for a work means the preferred form of the work for
156 | making modifications to it. For an executable work, complete source
157 | code means all the source code for all modules it contains, plus any
158 | associated interface definition files, plus the scripts used to
159 | control compilation and installation of the executable. However, as a
160 | special exception, the source code distributed need not include
161 | anything that is normally distributed (in either source or binary
162 | form) with the major components (compiler, kernel, and so on) of the
163 | operating system on which the executable runs, unless that component
164 | itself accompanies the executable.
165 |
166 | If distribution of executable or object code is made by offering
167 | access to copy from a designated place, then offering equivalent
168 | access to copy the source code from the same place counts as
169 | distribution of the source code, even though third parties are not
170 | compelled to copy the source along with the object code.
171 |
172 | 4. You may not copy, modify, sublicense, or distribute the Program
173 | except as expressly provided under this License. Any attempt
174 | otherwise to copy, modify, sublicense or distribute the Program is
175 | void, and will automatically terminate your rights under this License.
176 | However, parties who have received copies, or rights, from you under
177 | this License will not have their licenses terminated so long as such
178 | parties remain in full compliance.
179 |
180 | 5. You are not required to accept this License, since you have not
181 | signed it. However, nothing else grants you permission to modify or
182 | distribute the Program or its derivative works. These actions are
183 | prohibited by law if you do not accept this License. Therefore, by
184 | modifying or distributing the Program (or any work based on the
185 | Program), you indicate your acceptance of this License to do so, and
186 | all its terms and conditions for copying, distributing or modifying
187 | the Program or works based on it.
188 |
189 | 6. Each time you redistribute the Program (or any work based on the
190 | Program), the recipient automatically receives a license from the
191 | original licensor to copy, distribute or modify the Program subject to
192 | these terms and conditions. You may not impose any further
193 | restrictions on the recipients' exercise of the rights granted herein.
194 | You are not responsible for enforcing compliance by third parties to
195 | this License.
196 |
197 | 7. If, as a consequence of a court judgment or allegation of patent
198 | infringement or for any other reason (not limited to patent issues),
199 | conditions are imposed on you (whether by court order, agreement or
200 | otherwise) that contradict the conditions of this License, they do not
201 | excuse you from the conditions of this License. If you cannot
202 | distribute so as to satisfy simultaneously your obligations under this
203 | License and any other pertinent obligations, then as a consequence you
204 | may not distribute the Program at all. For example, if a patent
205 | license would not permit royalty-free redistribution of the Program by
206 | all those who receive copies directly or indirectly through you, then
207 | the only way you could satisfy both it and this License would be to
208 | refrain entirely from distribution of the Program.
209 |
210 | If any portion of this section is held invalid or unenforceable under
211 | any particular circumstance, the balance of the section is intended to
212 | apply and the section as a whole is intended to apply in other
213 | circumstances.
214 |
215 | It is not the purpose of this section to induce you to infringe any
216 | patents or other property right claims or to contest validity of any
217 | such claims; this section has the sole purpose of protecting the
218 | integrity of the free software distribution system, which is
219 | implemented by public license practices. Many people have made
220 | generous contributions to the wide range of software distributed
221 | through that system in reliance on consistent application of that
222 | system; it is up to the author/donor to decide if he or she is willing
223 | to distribute software through any other system and a licensee cannot
224 | impose that choice.
225 |
226 | This section is intended to make thoroughly clear what is believed to
227 | be a consequence of the rest of this License.
228 |
229 | 8. If the distribution and/or use of the Program is restricted in
230 | certain countries either by patents or by copyrighted interfaces, the
231 | original copyright holder who places the Program under this License
232 | may add an explicit geographical distribution limitation excluding
233 | those countries, so that distribution is permitted only in or among
234 | countries not thus excluded. In such case, this License incorporates
235 | the limitation as if written in the body of this License.
236 |
237 | 9. The Free Software Foundation may publish revised and/or new versions
238 | of the General Public License from time to time. Such new versions will
239 | be similar in spirit to the present version, but may differ in detail to
240 | address new problems or concerns.
241 |
242 | Each version is given a distinguishing version number. If the Program
243 | specifies a version number of this License which applies to it and "any
244 | later version", you have the option of following the terms and conditions
245 | either of that version or of any later version published by the Free
246 | Software Foundation. If the Program does not specify a version number of
247 | this License, you may choose any version ever published by the Free Software
248 | Foundation.
249 |
250 | 10. If you wish to incorporate parts of the Program into other free
251 | programs whose distribution conditions are different, write to the author
252 | to ask for permission. For software which is copyrighted by the Free
253 | Software Foundation, write to the Free Software Foundation; we sometimes
254 | make exceptions for this. Our decision will be guided by the two goals
255 | of preserving the free status of all derivatives of our free software and
256 | of promoting the sharing and reuse of software generally.
257 |
258 | NO WARRANTY
259 |
260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268 | REPAIR OR CORRECTION.
269 |
270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278 | POSSIBILITY OF SUCH DAMAGES.
279 |
280 | END OF TERMS AND CONDITIONS
281 |
282 | How to Apply These Terms to Your New Programs
283 |
284 | If you develop a new program, and you want it to be of the greatest
285 | possible use to the public, the best way to achieve this is to make it
286 | free software which everyone can redistribute and change under these terms.
287 |
288 | To do so, attach the following notices to the program. It is safest
289 | to attach them to the start of each source file to most effectively
290 | convey the exclusion of warranty; and each file should have at least
291 | the "copyright" line and a pointer to where the full notice is found.
292 |
293 | {description}
294 | Copyright (C) {year} {fullname}
295 |
296 | This program is free software; you can redistribute it and/or modify
297 | it under the terms of the GNU General Public License as published by
298 | the Free Software Foundation; either version 2 of the License, or
299 | (at your option) any later version.
300 |
301 | This program is distributed in the hope that it will be useful,
302 | but WITHOUT ANY WARRANTY; without even the implied warranty of
303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304 | GNU General Public License for more details.
305 |
306 | You should have received a copy of the GNU General Public License along
307 | with this program; if not, write to the Free Software Foundation, Inc.,
308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309 |
310 | Also add information on how to contact you by electronic and paper mail.
311 |
312 | If the program is interactive, make it output a short notice like this
313 | when it starts in an interactive mode:
314 |
315 | Gnomovision version 69, Copyright (C) year name of author
316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317 | This is free software, and you are welcome to redistribute it
318 | under certain conditions; type `show c' for details.
319 |
320 | The hypothetical commands `show w' and `show c' should show the appropriate
321 | parts of the General Public License. Of course, the commands you use may
322 | be called something other than `show w' and `show c'; they could even be
323 | mouse-clicks or menu items--whatever suits your program.
324 |
325 | You should also get your employer (if you work as a programmer) or your
326 | school, if any, to sign a "copyright disclaimer" for the program, if
327 | necessary. Here is a sample; alter the names:
328 |
329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330 | `Gnomovision' (which makes passes at compilers) written by James Hacker.
331 |
332 | {signature of Ty Coon}, 1 April 1989
333 | Ty Coon, President of Vice
334 |
335 | This General Public License does not permit incorporating your program into
336 | proprietary programs. If your program is a subroutine library, you may
337 | consider it more useful to permit linking proprietary applications with the
338 | library. If this is what you want to do, use the GNU Lesser General
339 | Public License instead of this License.
340 |
341 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Ion Auth ACL
2 | ### A handy access control list plugin for Ion Auth 2
3 | by [Steve Goodwin](https://uk.linkedin.com/pub/steve-goodwin/11/979/91a)
4 |
5 | Ben Edmunds did an amazing job at putting together a secure and well documented authentication system called Ion Auth, further to this I required a plugin which would work with this library and provide a full ACL (Access Control List) which add's a full permissions based layer below users groups.
6 |
7 | This allows for fine grain control over users where both users and groups can have their own permissions to allow / deny actions against them. This library was inspired by a NetTuts tutorial by Andrew Steenbuck (http://code.tutsplus.com/tutorials/a-better-login-system--net-3461).
8 |
9 | ## Documentation
10 | Documentation is coming, I just thought i'd get a release out to get some feedback for anyone wanting to use this plugin.
11 |
12 | ## Installation
13 |
14 | First of all you'll need to grab a fresh install of codeigniter (v2.0.2 and above), you'll then need to grab a copy of Ben's Ion Auth and follow the install instructions for that (https://github.com/benedmunds/CodeIgniter-Ion-Auth).
15 |
16 | Once you've got Ion Auth successfully integrated and working with your existing project or fresh install of CI then just grab this repo and overlay the files in their respective directories and install the sql/install.php with your favourite database editor.
17 |
18 | ### CodeIgniter Version 3 Compatibility
19 |
20 | CodeIgniter v3 requires the class names to be ucfirst(). In order to support this follow the standard installation procedures and then either rename the following files or create symlinks
21 |
22 | models/ion_auth_acl_model.php => models/Ion_auth_acl_model.php
23 |
24 | ## Usage
25 |
26 | In the package you will find example usage code in the controllers and views
27 | folders. The example code isn't the most beautiful code you'll ever see but
28 | it'll show you how to use the library and it's nice and generic so it doesn't
29 | require a MY_controller or anything else.
30 |
31 | You'll need to first create some permissions and assign them to groups and for user specific permissions you'll need to add those into the users permissions. If your adding these manually to start with then the value in the users / groups permissions tables is 1 for allow and 0 for deny.
32 |
33 | ## Help
34 |
35 | Feel free to send me an email if you have any problems.
36 |
37 |
38 | Thanks,
39 | -Steve Goodwin
40 | code@weblogics.com
41 | @steveg1987
42 |
--------------------------------------------------------------------------------
/config/ion_auth_acl.php:
--------------------------------------------------------------------------------
1 | ion_auth->logged_in() )
17 | redirect('/login');
18 |
19 | if( ! $this->ion_auth_acl->has_permission('access_admin') )
20 | redirect('/dashboard');
21 | }
22 |
23 | public function index()
24 | {
25 | redirect('/admin/manage');
26 | }
27 |
28 | public function manage()
29 | {
30 | $this->load->view('admin/manage');
31 | }
32 |
33 | public function permissions()
34 | {
35 | $data['permissions'] = $this->ion_auth_acl->permissions('full');
36 |
37 | $this->load->view('admin/permissions', $data);
38 | }
39 |
40 | public function add_permission()
41 | {
42 | if( $this->input->post() && $this->input->post('cancel') )
43 | redirect('/admin/permissions', 'refresh');
44 |
45 | $this->form_validation->set_rules('perm_key', 'key', 'required|trim');
46 | $this->form_validation->set_rules('perm_name', 'name', 'required|trim');
47 |
48 | $this->form_validation->set_message('required', 'Please enter a %s');
49 |
50 | if( $this->form_validation->run() === FALSE )
51 | {
52 | $data['message'] = ($this->ion_auth_acl->errors() ? $this->ion_auth_acl->errors() : $this->session->flashdata('message'));
53 |
54 | $this->load->view('admin/add_permission', $data);
55 | }
56 | else
57 | {
58 | $new_permission_id = $this->ion_auth_acl->create_permission($this->input->post('perm_key'), $this->input->post('perm_name'));
59 | if($new_permission_id)
60 | {
61 | // check to see if we are creating the permission
62 | // redirect them back to the admin page
63 | $this->session->set_flashdata('message', $this->ion_auth->messages());
64 | redirect("/admin/permissions", 'refresh');
65 | }
66 | }
67 | }
68 |
69 | public function update_permission()
70 | {
71 | if( $this->input->post() && $this->input->post('cancel') )
72 | redirect('admin/permissions', 'refresh');
73 |
74 | $permission_id = $this->uri->segment(3);
75 |
76 | if( ! $permission_id )
77 | {
78 | $this->session->set_flashdata('message', "No permission ID passed");
79 | redirect("admin/permissions", 'refresh');
80 | }
81 |
82 | $permission = $this->ion_auth_acl->permission($permission_id);
83 |
84 | $this->form_validation->set_rules('perm_key', 'key', 'required|trim');
85 | $this->form_validation->set_rules('perm_name', 'name', 'required|trim');
86 |
87 | $this->form_validation->set_message('required', 'Please enter a %s');
88 |
89 | if( $this->form_validation->run() === FALSE )
90 | {
91 | $data['message'] = ($this->ion_auth_acl->errors() ? $this->ion_auth_acl->errors() : $this->session->flashdata('message'));
92 | $data['permission'] = $permission;
93 |
94 | $this->load->view('admin/edit_permission', $data);
95 | }
96 | else
97 | {
98 | $additional_data = array(
99 | 'perm_name' => $this->input->post('perm_name')
100 | );
101 |
102 | $update_permission = $this->ion_auth_acl->update_permission($permission_id, $this->input->post('perm_key'), $additional_data);
103 | if($update_permission)
104 | {
105 | // check to see if we are creating the permission
106 | // redirect them back to the admin page
107 | $this->session->set_flashdata('message', $this->ion_auth->messages());
108 | redirect("/admin/permissions", 'refresh');
109 | }
110 | }
111 | }
112 |
113 | public function delete_permission()
114 | {
115 | if( $this->input->post() && $this->input->post('cancel') )
116 | redirect('/admin/permissions', 'refresh');
117 |
118 | $permission_id = $this->uri->segment(3);
119 |
120 | if( ! $permission_id )
121 | {
122 | $this->session->set_flashdata('message', "No permission ID passed");
123 | redirect("admin/permissions", 'refresh');
124 | }
125 |
126 | if( $this->input->post() && $this->input->post('delete') )
127 | {
128 | if( $this->ion_auth_acl->remove_permission($permission_id) )
129 | {
130 | $this->session->set_flashdata('message', $this->ion_auth->messages());
131 | redirect("admin/permissions", 'refresh');
132 | }
133 | else
134 | {
135 | echo $this->ion_auth_acl->messages();
136 | }
137 | }
138 | else
139 | {
140 | $data['message'] = ($this->ion_auth_acl->errors() ? $this->ion_auth_acl->errors() : $this->session->flashdata('message'));
141 |
142 | $this->load->view('/admin/delete_permission', $data);
143 | }
144 | }
145 |
146 | public function groups()
147 | {
148 | $data['groups'] = $this->ion_auth->groups()->result();
149 |
150 | $this->load->view('/admin/groups', $data);
151 | }
152 |
153 | public function group_permissions()
154 | {
155 | if( $this->input->post() && $this->input->post('cancel') )
156 | redirect('/admin/groups', 'refresh');
157 |
158 | $group_id = $this->uri->segment(3);
159 |
160 | if( ! $group_id )
161 | {
162 | $this->session->set_flashdata('message', "No group ID passed");
163 | redirect("admin/groups", 'refresh');
164 | }
165 |
166 | if( $this->input->post() && $this->input->post('save') )
167 | {
168 | foreach($this->input->post() as $k => $v)
169 | {
170 | if( substr($k, 0, 5) == 'perm_' )
171 | {
172 | $permission_id = str_replace("perm_","",$k);
173 |
174 | if( $v == "X" )
175 | $this->ion_auth_acl->remove_permission_from_group($group_id, $permission_id);
176 | else
177 | $this->ion_auth_acl->add_permission_to_group($group_id, $permission_id, $v);
178 | }
179 | }
180 |
181 | redirect('/admin/groups', 'refresh');
182 | }
183 |
184 | $data['permissions'] = $this->ion_auth_acl->permissions('full', 'perm_key');
185 | $data['group_permissions'] = $this->ion_auth_acl->get_group_permissions($group_id);
186 |
187 | $this->load->view('/admin/group_permissions', $data);
188 | }
189 |
190 | public function users()
191 | {
192 | $data['users'] = $this->ion_auth->users()->result();
193 |
194 | $this->load->view('/admin/users', $data);
195 | }
196 |
197 | public function manage_user()
198 | {
199 | $user_id = $this->uri->segment(3);
200 |
201 | if( ! $user_id )
202 | {
203 | $this->session->set_flashdata('message', "No user ID passed");
204 | redirect("admin/users", 'refresh');
205 | }
206 |
207 | $data['user'] = $this->ion_auth->user($user_id)->row();
208 | $data['user_groups'] = $this->ion_auth->get_users_groups($user_id)->result();
209 | $data['user_acl'] = $this->ion_auth_acl->build_acl($user_id);
210 |
211 | $this->load->view('/admin/manage_user', $data);
212 | }
213 |
214 | public function user_permissions()
215 | {
216 | $user_id = $this->uri->segment(3);
217 |
218 | if( ! $user_id )
219 | {
220 | $this->session->set_flashdata('message', "No user ID passed");
221 | redirect("admin/users", 'refresh');
222 | }
223 |
224 | if( $this->input->post() && $this->input->post('cancel') )
225 | redirect("/admin/manage-user/{$user_id}", 'refresh');
226 |
227 |
228 | if( $this->input->post() && $this->input->post('save') )
229 | {
230 | foreach($this->input->post() as $k => $v)
231 | {
232 | if( substr($k, 0, 5) == 'perm_' )
233 | {
234 | $permission_id = str_replace("perm_","",$k);
235 |
236 | if( $v == "X" )
237 | $this->ion_auth_acl->remove_permission_from_user($user_id, $permission_id);
238 | else
239 | $this->ion_auth_acl->add_permission_to_user($user_id, $permission_id, $v);
240 | }
241 | }
242 |
243 | redirect("/admin/manage-user/{$user_id}", 'refresh');
244 | }
245 |
246 | $user_groups = $this->ion_auth_acl->get_user_groups($user_id);
247 |
248 | $data['user_id'] = $user_id;
249 | $data['permissions'] = $this->ion_auth_acl->permissions('full', 'perm_key');
250 | $data['group_permissions'] = $this->ion_auth_acl->get_group_permissions($user_groups);
251 | $data['users_permissions'] = $this->ion_auth_acl->build_acl($user_id);
252 |
253 | $this->load->view('/admin/user_permissions', $data);
254 | }
255 |
256 | }
--------------------------------------------------------------------------------
/controllers/Auth.php:
--------------------------------------------------------------------------------
1 | ion_auth->logged_in() ) redirect('/dashboard');
26 |
27 | $this->form_validation->set_rules('email', 'email', 'required|valid_email|trim');
28 | $this->form_validation->set_rules('password', 'password', 'required|trim');
29 |
30 | $this->form_validation->set_message('required', 'Please enter your %s');
31 |
32 | // Validate form
33 | if( $this->form_validation->run() )
34 | {
35 | $remember = (bool) $this->input->post('remember');
36 |
37 | // Check login
38 | if( $this->ion_auth->login($this->input->post('email'), $this->input->post('password'), $remember) )
39 | {
40 | // Login was successful
41 | redirect('/dashboard', 'refresh');
42 | }
43 | else
44 | {
45 | // Login was un-successful
46 | $this->session->set_flashdata('message', $this->ion_auth->errors());
47 | redirect('auth/login', 'refresh');
48 | }
49 | }
50 | else
51 | {
52 | $data['message'] = $this->session->flashdata('message');
53 |
54 | $this->load->view('auth/login', $data);
55 | }
56 | }
57 |
58 | public function logout()
59 | {
60 | if( $this->ion_auth->logout() )
61 | redirect('/login');
62 | else
63 | die("There was an error logging you out");
64 | }
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/controllers/Dashboard.php:
--------------------------------------------------------------------------------
1 | ion_auth->logged_in() )
17 | redirect('/login');
18 | }
19 |
20 | public function index()
21 | {
22 | $data['users_groups'] = $this->ion_auth->get_users_groups()->result();
23 | $data['users_permissions'] = $this->ion_auth_acl->build_acl();
24 |
25 | $this->load->view('admin/dashboard', $data);
26 | }
27 |
28 | }
--------------------------------------------------------------------------------
/language/english/ion_auth_acl_lang.php:
--------------------------------------------------------------------------------
1 | load->model('ion_auth_acl_model');
29 |
30 | if (count($config) > 0)
31 | foreach ($config as $key => $val)
32 | $this->{$key} = $val;
33 | else
34 | $this->user_id = ( $this->ion_auth->logged_in() ) ? $this->ion_auth->user()->row()->id : FALSE;
35 |
36 | if( $this->ion_auth->logged_in() )
37 | {
38 | $this->user_groups = $this->get_user_groups($this->user_id);
39 | $this->user_permissions = $this->build_acl();
40 | }
41 | }
42 |
43 | /**
44 | * __call
45 | *
46 | * Acts as a simple way to call model methods without loads of stupid alias'
47 | *
48 | **/
49 | public function __call($method, $arguments)
50 | {
51 | if (!method_exists( $this->ion_auth_acl_model, $method) )
52 | throw new Exception('Undefined method Ion_auth_acl::' . $method . '() called');
53 |
54 | return call_user_func_array( array($this->ion_auth_acl_model, $method), $arguments);
55 | }
56 |
57 | /**
58 | * __get
59 | *
60 | * Enables the use of CI super-global without having to define an extra variable.
61 | *
62 | * I can't remember where I first saw this, so thank you if you are the original author. -Militis
63 | *
64 | * @access public
65 | * @param $var
66 | * @return mixed
67 | */
68 | public function __get($var)
69 | {
70 | return get_instance()->$var;
71 | }
72 |
73 | /**
74 | * Has Permission
75 | *
76 | * Checks to see if a user should be granted a permission or not.
77 | *
78 | * @author Steve Goodwin
79 | * @param bool|FALSE $key
80 | * @param array $permissions
81 | * @return bool
82 | */
83 | public function has_permission($key = FALSE, $permissions = array())
84 | {
85 | if( ! $key ) return FALSE;
86 |
87 | $permissions || $permissions = $this->user_permissions;
88 |
89 | $key = strtolower($key);
90 |
91 | if( array_key_exists($key, $permissions) )
92 | return ( $permissions[$key]['value'] === '1' || $permissions[$key]['value'] === TRUE ) ? TRUE : FALSE;
93 | else
94 | return FALSE;
95 | }
96 |
97 | /**
98 | * Is Allowed
99 | *
100 | * Checks to see if a permission should be allowed.
101 | *
102 | * @author Steve Goodwin
103 | * @param bool|FALSE $key
104 | * @param array $permissions
105 | * @return bool
106 | */
107 | public function is_allowed($key = FALSE, $permissions = array())
108 | {
109 | if( ! $key ) return FALSE;
110 |
111 | $permissions || $permissions = $this->user_permissions;
112 |
113 | return ( $this->has_permission($key, $permissions) && ! $this->is_inherited($key, $permissions) ) ? TRUE : FALSE;
114 | }
115 |
116 | /**
117 | * Is Deny
118 | *
119 | * Checks to see if a permission should be denied.
120 | *
121 | * @author Steve Goodwin
122 | * @param bool|FALSE $key
123 | * @param array $permissions
124 | * @return bool
125 | */
126 | public function is_denied($key = FALSE, $permissions = array())
127 | {
128 | if( ! $key ) return FALSE;
129 |
130 | $permissions || $permissions = $this->user_permissions;
131 |
132 | if( array_key_exists($key, $permissions) )
133 | return ( $permissions[$key]['value'] === FALSE && $permissions[$key]['inherited'] != TRUE) ? TRUE : FALSE;
134 | else
135 | return FALSE;
136 | }
137 |
138 | /**
139 | * Is Inherited
140 | *
141 | * Checks to see if a permission is inherited or not.
142 | *
143 | * @author Steve Goodwin
144 | * @param bool|FALSE $key
145 | * @param array $permissions
146 | * @param string $sub_key
147 | * @return bool
148 | */
149 | public function is_inherited($key = FALSE, $permissions = array(), $sub_key = 'inherited')
150 | {
151 | if( ! $key ) return FALSE;
152 |
153 | $permissions || $permissions = $this->user_permissions;
154 |
155 | if( array_key_exists($key, $permissions) )
156 | return ( $permissions[$key][$sub_key] === TRUE ) ? TRUE : FALSE;
157 | else
158 | return FALSE;
159 | }
160 |
161 | }
162 |
--------------------------------------------------------------------------------
/models/ion_auth_acl_model.php:
--------------------------------------------------------------------------------
1 | load->database();
28 |
29 | $this->lang->load('ion_auth_acl');
30 |
31 | $this->load->config('ion_auth_acl', TRUE);
32 |
33 | // initialize additional db tables data
34 | $this->tables = array_merge($this->tables, $this->config->item('tables', 'ion_auth_acl'));
35 |
36 | // initialize additional db join data
37 | $this->join = array_merge($this->join, $this->config->item('join', 'ion_auth_acl'));
38 | }
39 |
40 | /**
41 | * Create Permission
42 | *
43 | * @author Steve Goodwin
44 | * @param bool|FALSE $perm_key
45 | * @param string $perm_name
46 | * @return bool
47 | */
48 | public function create_permission($perm_key = FALSE, $perm_name = '')
49 | {
50 | // bail if the permission key was not passed
51 | if( ! $perm_key)
52 | {
53 | $this->set_error('permissions_key_required');
54 | return FALSE;
55 | }
56 |
57 | // bail if the permission key already exists
58 | $existing_permissions = $this->db->get_where($this->tables['permissions'], array('perm_key' => $perm_key))->num_rows();
59 | if($existing_permissions !== 0)
60 | {
61 | $this->set_error('permissions_already_exists');
62 | return FALSE;
63 | }
64 |
65 | $data = array('perm_key'=>$perm_key,'perm_name'=>$perm_name);
66 |
67 | $this->trigger_events('extra_permission_set');
68 |
69 | // insert the new permission
70 | $this->db->insert($this->tables['permissions'], $data);
71 | $permission_id = $this->db->insert_id();
72 |
73 | // report success
74 | $this->set_message('permission_creation_successful');
75 | // return the brand new permission id
76 | return $permission_id;
77 | }
78 |
79 | /**
80 | * Update Permission
81 | *
82 | * @author Steve Goodwin
83 | * @param bool|FALSE $permission_id
84 | * @param bool|FALSE $perm_key
85 | * @param array $additional_data
86 | * @return bool
87 | */
88 | public function update_permission($permission_id = FALSE, $perm_key = FALSE, $additional_data = array())
89 | {
90 | if (empty($permission_id)) return FALSE;
91 |
92 | $data = array();
93 |
94 | if (!empty($perm_key))
95 | {
96 | // we are changing the perm key, so do some checks
97 |
98 | // bail if the perm key already exists
99 | $existing_permission = $this->db->get_where($this->tables['permissions'], array('perm_key' => $perm_key))->row();
100 | if(isset($existing_permission->id) && $existing_permission->id != $permission_id)
101 | {
102 | $this->set_error('permission_already_exists');
103 | return FALSE;
104 | }
105 |
106 | $data['perm_key'] = $perm_key;
107 | }
108 |
109 | // restrict change of perm key of the admin permission
110 | $permission = $this->db->get_where($this->tables['permissions'], array('id' => $permission_id))->row();
111 | if($this->config->item('admin_permission', 'ion_auth_acl') === $permission->perm_key && $perm_key !== $permission->perm_key)
112 | {
113 | $this->set_error('permission_key_admin_not_alter');
114 | return FALSE;
115 | }
116 |
117 |
118 | // IMPORTANT!! Third parameter was string type $description; this following code is to maintain backward compatibility
119 | // New projects should work with 3rd param as array
120 | if (is_string($additional_data)) $additional_data = array('perm_name' => $additional_data);
121 |
122 |
123 | // filter out any data passed that doesnt have a matching column in the permissions table
124 | // and merge the set permission data and the additional data
125 | if (!empty($additional_data)) $data = array_merge($this->_filter_data($this->tables['permissions'], $additional_data), $data);
126 |
127 |
128 | $this->db->update($this->tables['permissions'], $data, array('id' => $permission_id));
129 |
130 | $this->set_message('permission_update_successful');
131 |
132 | return TRUE;
133 | }
134 |
135 | /**
136 | * Remove Permission
137 | *
138 | * @author Steve Goodwin
139 | * @param bool|FALSE $permission_id
140 | * @return bool
141 | */
142 | public function remove_permission($permission_id = FALSE)
143 | {
144 | // bail if mandatory param not set
145 | if(!$permission_id || empty($permission_id))
146 | {
147 | return FALSE;
148 | }
149 | $permission = $this->permission($permission_id);
150 | if($permission->perm_key == $this->config->item('admin_permission', 'ion_auth_acl'))
151 | {
152 | $this->trigger_events(array('post_delete_permission', 'post_delete_permission_notallowed'));
153 | $this->set_error('permission_delete_notallowed');
154 | return FALSE;
155 | }
156 |
157 | $this->trigger_events('pre_delete_permission');
158 |
159 | $this->db->trans_begin();
160 |
161 | // remove all users from this permission
162 | $this->db->delete($this->tables['users_permissions'], array($this->join['permissions'] => $permission_id));
163 | // remove the permission itself
164 | $this->db->delete($this->tables['permissions'], array('id' => $permission_id));
165 |
166 | if ($this->db->trans_status() === FALSE)
167 | {
168 | $this->db->trans_rollback();
169 | $this->trigger_events(array('post_delete_permission', 'post_delete_permission_unsuccessful'));
170 | $this->set_error('permission_delete_unsuccessful');
171 | return FALSE;
172 | }
173 |
174 | $this->db->trans_commit();
175 |
176 | $this->trigger_events(array('post_delete_permission', 'post_delete_permission_successful'));
177 | $this->set_message('permission_delete_successful');
178 | return TRUE;
179 | }
180 |
181 | /**
182 | * Get User Groups
183 | *
184 | * Returns an ID based list of a specific users groups.
185 | *
186 | * @author Steve Goodwin
187 | * @param bool|FALSE $user_id
188 | * @return array
189 | */
190 | public function get_user_groups($user_id = FALSE)
191 | {
192 | $this->trigger_events('get_user_groups');
193 |
194 | $users_groups = array();
195 |
196 | foreach( $this->get_users_groups($user_id)->result() as $group )
197 | $users_groups[] = $group->id;
198 |
199 | return $users_groups;
200 | }
201 |
202 | /**
203 | * Get Groups
204 | *
205 | * Returns all groups in a formatted list.
206 | *
207 | * @author Steve Goodwin
208 | * @param string $format
209 | * @return array
210 | */
211 | public function get_groups($format = 'ids')
212 | {
213 | $this->trigger_events('get_groups');
214 |
215 | $groups = array();
216 |
217 | foreach( $this->groups()->result() as $group )
218 | if( $format == 'full' )
219 | $groups[] = array('id' => $group->id, 'name' => $group->name);
220 | else
221 | $groups[] = $group->id;
222 |
223 | return $groups;
224 | }
225 |
226 | /**
227 | * Permissions
228 | *
229 | * Returns all permissions in a formatted list.
230 | *
231 | * @param string $format
232 | * @param string $key
233 | * @return array
234 | */
235 | public function permissions($format = 'ids', $key = 'id')
236 | {
237 | $this->trigger_events('permissions');
238 |
239 | $this->db->order_by('perm_name', 'ASC');
240 |
241 | $query = $this->db->get($this->tables['permissions']);
242 | $result = $query->result();
243 |
244 | $permissions = array();
245 |
246 | foreach( $result as $permission )
247 | if( $format == 'full' )
248 | $permissions[$permission->{$key}] = array('id' => $permission->id, 'key' => $permission->perm_key, 'name' => $permission->perm_name);
249 | else
250 | $permissions[] = $permission->id;
251 |
252 | return $permissions;
253 | }
254 |
255 | /**
256 | * Permission
257 | *
258 | * Returns a specific permission based on a permission ID.
259 | *
260 | * @author Steve Goodwin
261 | * @param bool|FALSE $id
262 | * @return bool
263 | */
264 | public function permission($id = FALSE)
265 | {
266 | $this->trigger_events('permission');
267 |
268 | if( ! $id ) return FALSE;
269 |
270 | $this->db->where('id', $id);
271 |
272 | return $this->db->get($this->tables['permissions'])->row();
273 | }
274 |
275 | /**
276 | * Get User Permissions
277 | *
278 | * Returns a formatted list of user permissions.
279 | *
280 | * @param bool|FALSE $user_id
281 | * @return array
282 | */
283 | public function get_user_permissions($user_id = FALSE)
284 | {
285 | $this->trigger_events('get_user_permissions');
286 |
287 | // if no id was passed use the current users id
288 | $user_id || $user_id = $this->session->userdata('user_id');
289 |
290 | $this->db->where('user_id', $user_id);
291 | $this->db->order_by('created_at', 'ASC');
292 |
293 | $query = $this->db->get($this->tables['users_permissions']);
294 |
295 | $permissions = $this->permissions('full');
296 | $user_permissions = array();
297 |
298 | foreach( $query->result() as $perm )
299 | {
300 | $permission = ( array_key_exists($perm->perm_id, $permissions) ) ? $permissions[$perm->perm_id] : FALSE;
301 |
302 | if( ! $permission ) continue;
303 |
304 | $user_permissions[$permission['key']] = array(
305 | 'id' => $permission['id'],
306 | 'name' => $permission['name'],
307 | 'key' => $permission['key'],
308 | 'inherited' => FALSE,
309 | 'value' => ($perm->value == '1') ? TRUE : FALSE
310 | );
311 | }
312 |
313 | return $user_permissions;
314 | }
315 |
316 | /**
317 | * Add Permission To User
318 | *
319 | * @author Steve Goodwin
320 | * @param bool|FALSE $group_id
321 | * @param bool|FALSE $perm_id
322 | * @param int $value
323 | * @return bool
324 | */
325 | public function add_permission_to_user($user_id = FALSE, $perm_id = FALSE, $value = 0)
326 | {
327 | // bail if the user id & permission id were not passed
328 | if( ! $user_id)
329 | {
330 | $this->set_error('user_permissions_user_id_required');
331 | return FALSE;
332 | }
333 |
334 | if( ! $perm_id)
335 | {
336 | $this->set_error('user_permissions_permission_id_required');
337 | return FALSE;
338 | }
339 |
340 | $data = array('user_id' => $user_id, 'perm_id' => $perm_id);
341 |
342 | $existing_group_permission = $this->db->get_where($this->tables['users_permissions'], $data)->num_rows();
343 |
344 | $data['created_at'] = strtotime('now');
345 | $data['updated_at'] = strtotime('now');
346 | $data['value'] = $value;
347 |
348 | $this->db->trans_start();
349 |
350 | if( $existing_group_permission )
351 | $this->db->replace($this->tables['users_permissions'], $data);
352 | else
353 | $this->db->insert($this->tables['users_permissions'], $data);
354 |
355 | $this->db->trans_complete();
356 |
357 | if ($this->db->trans_status() === FALSE)
358 | {
359 | $this->db->trans_rollback();
360 | $this->set_message('user_permission_add_unsuccessful');
361 | return FALSE;
362 | }
363 | else
364 | {
365 | $this->set_message('user_permission_add_successful');
366 | return TRUE;
367 | }
368 | }
369 |
370 | /**
371 | * Remove Permission From User
372 | *
373 | * @author Steve Goodwin
374 | * @param bool|FALSE $user_id
375 | * @param bool|FALSE $perm_id
376 | * @return bool
377 | */
378 | public function remove_permission_from_user($user_id = FALSE, $perm_id = FALSE)
379 | {
380 | // bail if the user id was not passed
381 | if( ! $user_id)
382 | {
383 | $this->set_error('user_permissions_user_id_required');
384 | return FALSE;
385 | }
386 |
387 | if( ! $perm_id)
388 | {
389 | $this->set_error('user_permissions_permission_id_required');
390 | return FALSE;
391 | }
392 |
393 | $this->trigger_events('pre_delete_user_permission');
394 |
395 | $this->db->trans_begin();
396 |
397 | // remove permission from the user
398 | if( ! $this->db->delete($this->tables['users_permissions'], array('user_id' => $user_id, 'perm_id' => $perm_id)) )
399 | {
400 | $this->db->trans_rollback();
401 | $this->trigger_events(array('post_delete_user_permission', 'post_delete_user_permission_unsuccessful'));
402 | $this->set_error('user_permission_delete_unsuccessful');
403 | return FALSE;
404 | }
405 | else
406 | {
407 | $this->db->trans_commit();
408 | $this->trigger_events(array('post_delete_user_permission', 'post_delete_user_permission_successful'));
409 | $this->set_message('user_permission_delete_successful');
410 | return TRUE;
411 | }
412 | }
413 |
414 | /**
415 | * Add Permission To Group
416 | *
417 | * @author Steve Goodwin
418 | * @param bool|FALSE $group_id
419 | * @param bool|FALSE $perm_id
420 | * @param int $value
421 | * @return bool
422 | */
423 | public function add_permission_to_group($group_id = FALSE, $perm_id = FALSE, $value = 0)
424 | {
425 | // bail if the group id & permission id were not passed
426 | if( ! $group_id)
427 | {
428 | $this->set_error('group_permissions_group_id_required');
429 | return FALSE;
430 | }
431 |
432 | if( ! $perm_id)
433 | {
434 | $this->set_error('group_permissions_permission_id_required');
435 | return FALSE;
436 | }
437 |
438 | $data = array('group_id' => $group_id, 'perm_id' => $perm_id);
439 |
440 | $existing_group_permission = $this->db->get_where($this->tables['group_permissions'], $data)->num_rows();
441 |
442 | $data['created_at'] = strtotime('now');
443 | $data['updated_at'] = strtotime('now');
444 | $data['value'] = $value;
445 |
446 | $this->db->trans_start();
447 |
448 | if( $existing_group_permission )
449 | $this->db->replace($this->tables['group_permissions'], $data);
450 | else
451 | $this->db->insert($this->tables['group_permissions'], $data);
452 |
453 | $this->db->trans_complete();
454 |
455 | if ($this->db->trans_status() === FALSE)
456 | {
457 | $this->db->trans_rollback();
458 | $this->set_message('group_permission_add_unsuccessful');
459 | return FALSE;
460 | }
461 | else
462 | {
463 | $this->set_message('group_permission_add_successful');
464 | return TRUE;
465 | }
466 | }
467 |
468 | /**
469 | * Remove Permission From Group
470 | *
471 | * @author Steve Goodwin
472 | * @param bool|FALSE $group_id
473 | * @param bool|FALSE $perm_id
474 | * @return bool
475 | */
476 | public function remove_permission_from_group($group_id = FALSE, $perm_id = FALSE)
477 | {
478 | // bail if the group id & permission id were not passed
479 | if( ! $group_id)
480 | {
481 | $this->set_error('group_permissions_group_id_required');
482 | return FALSE;
483 | }
484 |
485 | if( ! $perm_id)
486 | {
487 | $this->set_error('group_permissions_permission_id_required');
488 | return FALSE;
489 | }
490 |
491 | $this->trigger_events('pre_delete_group_permission');
492 |
493 | $this->db->trans_begin();
494 |
495 | // remove permission from the group
496 | $this->db->delete($this->tables['group_permissions'], array('group_id' => $group_id, 'perm_id' => $perm_id));
497 |
498 | $this->db->trans_complete();
499 |
500 | if ($this->db->trans_status() === FALSE)
501 | {
502 | $this->db->trans_rollback();
503 | $this->trigger_events(array('post_delete_group_permission', 'post_delete_group_permission_unsuccessful'));
504 | $this->set_error('group_permission_delete_unsuccessful');
505 | return FALSE;
506 | }
507 | else
508 | {
509 | $this->trigger_events(array('post_delete_group_permission', 'post_delete_group_permission_successful'));
510 | $this->set_message('group_permission_delete_successful');
511 | return TRUE;
512 | }
513 | }
514 |
515 | /**
516 | * Get Group Permissions
517 | *
518 | * Returns a formatted list of group permissions.
519 | *
520 | * @param mixed|FALSE $group_id
521 | * @return array
522 | */
523 | public function get_group_permissions($group_id = FALSE)
524 | {
525 | $this->trigger_events('get_group_permissions');
526 |
527 | // Try to get the currently logged in users groups if none supplied
528 | if( ! $group_id )
529 | foreach($this->get_users_groups()->result() as $group)
530 | $group_id[] = $group->id;
531 |
532 | // Still No groups then theres nothing to do!
533 | if( ! $group_id )
534 | return FALSE;
535 |
536 | if( is_array($group_id) )
537 | $this->db->where_in('group_id', $group_id);
538 | else
539 | $this->db->where('group_id', $group_id);
540 |
541 | $query = $this->db->order_by('id', 'asc')
542 | ->get($this->tables['group_permissions']);
543 |
544 | $permissions = $this->permissions('full');
545 | $group_permissions = array();
546 |
547 | foreach( $query->result() as $perm )
548 | {
549 | $permission = ( array_key_exists($perm->perm_id, $permissions) ) ? $permissions[$perm->perm_id] : FALSE;
550 |
551 | if( ! $permission ) continue;
552 |
553 | $group_permissions[$permission['key']] = array(
554 | 'id' => $permission['id'],
555 | 'name' => $permission['name'],
556 | 'key' => $permission['key'],
557 | 'inherited' => TRUE,
558 | 'value' => ($perm->value === '1') ? TRUE : FALSE,
559 | );
560 | }
561 |
562 | return $group_permissions;
563 | }
564 |
565 | /**
566 | * Build ACL
567 | *
568 | * Gets all user & group permissions and merges them to build a full
569 | * list of permissions a specific user.
570 | *
571 | * @author Steve Goodwin
572 | * @param bool|FALSE $user_id
573 | * @return array
574 | */
575 | public function build_acl($user_id = FALSE)
576 | {
577 | $user_permissions = $this->get_user_permissions($user_id);
578 | $user_groups = $this->get_user_groups($user_id);
579 | $group_permissions = $this->get_group_permissions($user_groups);
580 |
581 | $permissions = array();
582 |
583 | if (count($user_groups) > 0)
584 | $permissions = array_merge($permissions, $group_permissions);
585 |
586 | $permissions = array_merge($permissions, $user_permissions);
587 |
588 | return $permissions;
589 | }
590 |
591 | }
592 |
--------------------------------------------------------------------------------
/sql/install.sql:
--------------------------------------------------------------------------------
1 | -- phpMyAdmin SQL Dump
2 | -- version 4.4.2
3 | -- http://www.phpmyadmin.net
4 | --
5 | -- Host: 127.0.0.1
6 | -- Generation Time: Sep 28, 2015 at 01:17 PM
7 | -- Server version: 5.6.24
8 | -- PHP Version: 5.6.8
9 |
10 | SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
11 | SET time_zone = "+00:00";
12 |
13 |
14 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
15 | /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
16 | /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
17 | /*!40101 SET NAMES utf8 */;
18 |
19 | --
20 | -- Database: `ci_acl_db`
21 | --
22 |
23 | -- --------------------------------------------------------
24 |
25 | --
26 | -- Table structure for table `groups_permissions`
27 | --
28 |
29 | CREATE TABLE IF NOT EXISTS `groups_permissions` (
30 | `id` int(11) NOT NULL,
31 | `group_id` int(11) NOT NULL,
32 | `perm_id` int(11) NOT NULL,
33 | `value` tinyint(4) DEFAULT '0',
34 | `created_at` int(11) NOT NULL,
35 | `updated_at` int(11) NOT NULL
36 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
37 |
38 | -- --------------------------------------------------------
39 |
40 | --
41 | -- Table structure for table `permissions`
42 | --
43 |
44 | CREATE TABLE IF NOT EXISTS `permissions` (
45 | `id` int(11) NOT NULL,
46 | `perm_key` varchar(30) NOT NULL,
47 | `perm_name` varchar(100) NOT NULL
48 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
49 |
50 | -- --------------------------------------------------------
51 |
52 | --
53 | -- Table structure for table `users_permissions`
54 | --
55 |
56 | CREATE TABLE IF NOT EXISTS `users_permissions` (
57 | `id` int(11) NOT NULL,
58 | `user_id` int(11) NOT NULL,
59 | `perm_id` int(11) NOT NULL,
60 | `value` tinyint(1) NOT NULL DEFAULT '0',
61 | `created_at` int(11) NOT NULL,
62 | `updated_at` int(11) NOT NULL
63 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
64 |
65 | --
66 | -- Indexes for dumped tables
67 | --
68 |
69 | --
70 | -- Indexes for table `groups_permissions`
71 | --
72 | ALTER TABLE `groups_permissions`
73 | ADD PRIMARY KEY (`id`),
74 | ADD UNIQUE KEY `roleID_2` (`group_id`,`perm_id`);
75 |
76 | --
77 | -- Indexes for table `permissions`
78 | --
79 | ALTER TABLE `permissions`
80 | ADD PRIMARY KEY (`id`),
81 | ADD UNIQUE KEY `permKey` (`perm_key`);
82 |
83 | --
84 | -- Indexes for table `users_permissions`
85 | --
86 | ALTER TABLE `users_permissions`
87 | ADD PRIMARY KEY (`id`),
88 | ADD UNIQUE KEY `userID` (`user_id`,`perm_id`);
89 |
90 | --
91 | -- AUTO_INCREMENT for dumped tables
92 | --
93 |
94 | --
95 | -- AUTO_INCREMENT for table `groups_permissions`
96 | --
97 | ALTER TABLE `groups_permissions`
98 | MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
99 | --
100 | -- AUTO_INCREMENT for table `permissions`
101 | --
102 | ALTER TABLE `permissions`
103 | MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
104 | --
105 | -- AUTO_INCREMENT for table `users_permissions`
106 | --
107 | ALTER TABLE `users_permissions`
108 | MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
109 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
110 | /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
111 | /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
112 |
--------------------------------------------------------------------------------
/views/admin/add_permission.php:
--------------------------------------------------------------------------------
1 |