-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathmemory.c
948 lines (773 loc) · 30.1 KB
/
memory.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2014 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to license@phalconphp.com so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <andres@phalconphp.com> |
| Eduar Carvajal <eduar@phalconphp.com> |
+------------------------------------------------------------------------+
*/
#include "php_phalcon.h"
#include "acl/adapter/memory.h"
#include "acl/adapter.h"
#include "acl/adapterinterface.h"
#include "acl/exception.h"
#include "acl/resource.h"
#include "acl/role.h"
#include "kernel/main.h"
#include "kernel/memory.h"
#include "kernel/array.h"
#include "kernel/object.h"
#include "kernel/fcall.h"
#include "kernel/concat.h"
#include "kernel/exception.h"
#include "kernel/operators.h"
#include "kernel/hash.h"
/**
* Phalcon\Acl\Adapter\Memory
*
* Manages ACL lists in memory
*
*<code>
*
* $acl = new Phalcon\Acl\Adapter\Memory();
*
* $acl->setDefaultAction(Phalcon\Acl::DENY);
*
* //Register roles
* $roles = array(
* 'users' => new Phalcon\Acl\Role('Users'),
* 'guests' => new Phalcon\Acl\Role('Guests')
* );
* foreach ($roles as $role) {
* $acl->addRole($role);
* }
*
* //Private area resources
* $privateResources = array(
* 'companies' => array('index', 'search', 'new', 'edit', 'save', 'create', 'delete'),
* 'products' => array('index', 'search', 'new', 'edit', 'save', 'create', 'delete'),
* 'invoices' => array('index', 'profile')
* );
* foreach ($privateResources as $resource => $actions) {
* $acl->addResource(new Phalcon\Acl\Resource($resource), $actions);
* }
*
* //Public area resources
* $publicResources = array(
* 'index' => array('index'),
* 'about' => array('index'),
* 'session' => array('index', 'register', 'start', 'end'),
* 'contact' => array('index', 'send')
* );
* foreach ($publicResources as $resource => $actions) {
* $acl->addResource(new Phalcon\Acl\Resource($resource), $actions);
* }
*
* //Grant access to public areas to both users and guests
* foreach ($roles as $role){
* foreach ($publicResources as $resource => $actions) {
* $acl->allow($role->getName(), $resource, '*');
* }
* }
*
* //Grant access to private area to role Users
* foreach ($privateResources as $resource => $actions) {
* foreach ($actions as $action) {
* $acl->allow('Users', $resource, $action);
* }
* }
*
*</code>
*/
zend_class_entry *phalcon_acl_adapter_memory_ce;
PHP_METHOD(Phalcon_Acl_Adapter_Memory, __construct);
PHP_METHOD(Phalcon_Acl_Adapter_Memory, addRole);
PHP_METHOD(Phalcon_Acl_Adapter_Memory, addInherit);
PHP_METHOD(Phalcon_Acl_Adapter_Memory, isRole);
PHP_METHOD(Phalcon_Acl_Adapter_Memory, isResource);
PHP_METHOD(Phalcon_Acl_Adapter_Memory, addResource);
PHP_METHOD(Phalcon_Acl_Adapter_Memory, addResourceAccess);
PHP_METHOD(Phalcon_Acl_Adapter_Memory, dropResourceAccess);
PHP_METHOD(Phalcon_Acl_Adapter_Memory, _allowOrDeny);
PHP_METHOD(Phalcon_Acl_Adapter_Memory, allow);
PHP_METHOD(Phalcon_Acl_Adapter_Memory, deny);
PHP_METHOD(Phalcon_Acl_Adapter_Memory, isAllowed);
PHP_METHOD(Phalcon_Acl_Adapter_Memory, getRoles);
PHP_METHOD(Phalcon_Acl_Adapter_Memory, getResources);
static const zend_function_entry phalcon_acl_adapter_memory_method_entry[] = {
PHP_ME(Phalcon_Acl_Adapter_Memory, __construct, arginfo_phalcon_acl_adapterinterface_empty, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(Phalcon_Acl_Adapter_Memory, addRole, arginfo_phalcon_acl_adapterinterface_addrole, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Acl_Adapter_Memory, addInherit, arginfo_phalcon_acl_adapterinterface_addinherit, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Acl_Adapter_Memory, isRole, arginfo_phalcon_acl_adapterinterface_isrole, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Acl_Adapter_Memory, isResource, arginfo_phalcon_acl_adapterinterface_isresource, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Acl_Adapter_Memory, addResource, arginfo_phalcon_acl_adapterinterface_addresource, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Acl_Adapter_Memory, addResourceAccess, arginfo_phalcon_acl_adapterinterface_addresourceaccess, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Acl_Adapter_Memory, dropResourceAccess, arginfo_phalcon_acl_adapterinterface_dropresourceaccess, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Acl_Adapter_Memory, _allowOrDeny, arginfo_phalcon_acl_adapterinterface_empty, ZEND_ACC_PROTECTED)
PHP_ME(Phalcon_Acl_Adapter_Memory, allow, arginfo_phalcon_acl_adapterinterface_allow, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Acl_Adapter_Memory, deny, arginfo_phalcon_acl_adapterinterface_allow, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Acl_Adapter_Memory, isAllowed, arginfo_phalcon_acl_adapterinterface_isallowed, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Acl_Adapter_Memory, getRoles, arginfo_phalcon_acl_adapterinterface_empty, ZEND_ACC_PUBLIC)
PHP_ME(Phalcon_Acl_Adapter_Memory, getResources, arginfo_phalcon_acl_adapterinterface_empty, ZEND_ACC_PUBLIC)
PHP_FE_END
};
/**
* Phalcon\Acl\Adapter\Memory initializer
*/
PHALCON_INIT_CLASS(Phalcon_Acl_Adapter_Memory){
PHALCON_REGISTER_CLASS_EX(Phalcon\\Acl\\Adapter, Memory, acl_adapter_memory, phalcon_acl_adapter_ce, phalcon_acl_adapter_memory_method_entry, 0);
zend_declare_property_null(phalcon_acl_adapter_memory_ce, SL("_rolesNames"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_acl_adapter_memory_ce, SL("_roles"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_acl_adapter_memory_ce, SL("_resourcesNames"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_acl_adapter_memory_ce, SL("_resources"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_acl_adapter_memory_ce, SL("_access"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_acl_adapter_memory_ce, SL("_roleInherits"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(phalcon_acl_adapter_memory_ce, SL("_accessList"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_class_implements(phalcon_acl_adapter_memory_ce TSRMLS_CC, 1, phalcon_acl_adapterinterface_ce);
return SUCCESS;
}
/**
* Phalcon\Acl\Adapter\Memory constructor
*
*/
PHP_METHOD(Phalcon_Acl_Adapter_Memory, __construct){
zval *resources_names, *access_list;
PHALCON_MM_GROW();
PHALCON_INIT_VAR(resources_names);
array_init_size(resources_names, 1);
phalcon_array_update_string(&resources_names, SL("*"), PHALCON_GLOBAL(z_true), PH_COPY);
phalcon_update_property_this(this_ptr, SL("_resourcesNames"), resources_names TSRMLS_CC);
PHALCON_INIT_VAR(access_list);
array_init_size(access_list, 1);
phalcon_array_update_string(&access_list, SL("*!*"), PHALCON_GLOBAL(z_true), PH_COPY);
phalcon_update_property_this(this_ptr, SL("_accessList"), access_list TSRMLS_CC);
PHALCON_MM_RESTORE();
}
/**
* Adds a role to the ACL list. Second parameter allows inheriting access data from other existing role
*
* Example:
* <code>
* $acl->addRole(new Phalcon\Acl\Role('administrator'), 'consultant');
* $acl->addRole('administrator', 'consultant');
* </code>
*
* @param Phalcon\Acl\RoleInterface $role
* @param array|string $accessInherits
* @return boolean
*/
PHP_METHOD(Phalcon_Acl_Adapter_Memory, addRole){
zval *role, *access_inherits = NULL, *role_name = NULL, *object = NULL;
zval *roles_names, *default_access;
zval *key;
PHALCON_MM_GROW();
phalcon_fetch_params(1, 1, 1, &role, &access_inherits);
if (!access_inherits) {
access_inherits = PHALCON_GLOBAL(z_null);
}
if (Z_TYPE_P(role) == IS_OBJECT) {
PHALCON_INIT_VAR(role_name);
phalcon_call_method(role_name, role, "getname");
PHALCON_CPY_WRT(object, role);
} else {
PHALCON_CPY_WRT(role_name, role);
PHALCON_INIT_NVAR(object);
object_init_ex(object, phalcon_acl_role_ce);
phalcon_call_method_p1_noret(object, "__construct", role);
}
roles_names = phalcon_fetch_nproperty_this(this_ptr, SL("_rolesNames"), PH_NOISY_CC);
if (phalcon_array_isset(roles_names, role_name)) {
RETURN_MM_FALSE;
}
phalcon_update_property_array_append(this_ptr, SL("_roles"), object TSRMLS_CC);
phalcon_update_property_array(this_ptr, SL("_rolesNames"), role_name, PHALCON_GLOBAL(z_true) TSRMLS_CC);
default_access = phalcon_fetch_nproperty_this(this_ptr, SL("_defaultAccess"), PH_NOISY_CC);
PHALCON_INIT_VAR(key);
PHALCON_CONCAT_VS(key, role_name, "!*!*");
phalcon_update_property_array(this_ptr, SL("_access"), key, default_access TSRMLS_CC);
if (Z_TYPE_P(access_inherits) != IS_NULL) {
phalcon_call_method_p2(return_value, this_ptr, "addinherit", role_name, access_inherits);
RETURN_MM();
}
RETURN_MM_TRUE;
}
/**
* Do a role inherit from another existing role
*
* @param string $roleName
* @param string $roleToInherit
*/
PHP_METHOD(Phalcon_Acl_Adapter_Memory, addInherit){
zval *role_name, *role_to_inherit, *roles_names;
zval *exception_message = NULL, *role_inherit_name = NULL;
zval *roles_inherits, *empty_arr, *_roleInherits;
PHALCON_MM_GROW();
phalcon_fetch_params(1, 2, 0, &role_name, &role_to_inherit);
PHALCON_OBS_VAR(roles_names);
phalcon_read_property_this(&roles_names, this_ptr, SL("_rolesNames"), PH_NOISY_CC);
if (!phalcon_array_isset(roles_names, role_name)) {
PHALCON_INIT_VAR(exception_message);
PHALCON_CONCAT_SVS(exception_message, "Role '", role_name, "' does not exist in the role list");
PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message);
return;
}
if (Z_TYPE_P(role_to_inherit) == IS_OBJECT) {
PHALCON_INIT_VAR(role_inherit_name);
phalcon_call_method(role_inherit_name, role_to_inherit, "getname");
} else {
PHALCON_CPY_WRT(role_inherit_name, role_to_inherit);
}
/**
* Check if the role to inherit is valid
*/
if (!phalcon_array_isset(roles_names, role_inherit_name)) {
PHALCON_INIT_NVAR(exception_message);
PHALCON_CONCAT_SVS(exception_message, "Role '", role_inherit_name, "' (to inherit) does not exist in the role list");
PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message);
return;
}
if (PHALCON_IS_EQUAL(role_inherit_name, role_name)) {
RETURN_MM_FALSE;
}
PHALCON_OBS_VAR(roles_inherits);
phalcon_read_property_this(&roles_inherits, this_ptr, SL("_roleInherits"), PH_NOISY_CC);
if (!phalcon_array_isset(roles_inherits, role_name)) {
PHALCON_INIT_VAR(empty_arr);
array_init(empty_arr);
phalcon_update_property_array(this_ptr, SL("_roleInherits"), role_name, empty_arr TSRMLS_CC);
}
PHALCON_OBS_VAR(_roleInherits);
phalcon_read_property_this(&_roleInherits, this_ptr, SL("_roleInherits"), PH_NOISY_CC);
phalcon_array_append_multi_2(&_roleInherits, role_name, role_inherit_name, 0);
phalcon_update_property_this(this_ptr, SL("_roleInherits"), _roleInherits TSRMLS_CC);
RETURN_MM_TRUE;
}
/**
* Check whether role exist in the roles list
*
* @param string $roleName
* @return boolean
*/
PHP_METHOD(Phalcon_Acl_Adapter_Memory, isRole){
zval *role_name, *roles_names;
PHALCON_MM_GROW();
phalcon_fetch_params(1, 1, 0, &role_name);
PHALCON_OBS_VAR(roles_names);
phalcon_read_property_this(&roles_names, this_ptr, SL("_rolesNames"), PH_NOISY_CC);
if (phalcon_array_isset(roles_names, role_name)) {
RETURN_MM_TRUE;
}
RETURN_MM_FALSE;
}
/**
* Check whether resource exist in the resources list
*
* @param string $resourceName
* @return boolean
*/
PHP_METHOD(Phalcon_Acl_Adapter_Memory, isResource){
zval *resource_name, *resources_names;
PHALCON_MM_GROW();
phalcon_fetch_params(1, 1, 0, &resource_name);
PHALCON_OBS_VAR(resources_names);
phalcon_read_property_this(&resources_names, this_ptr, SL("_resourcesNames"), PH_NOISY_CC);
if (phalcon_array_isset(resources_names, resource_name)) {
RETURN_MM_TRUE;
}
RETURN_MM_FALSE;
}
/**
* Adds a resource to the ACL list
*
* Access names can be a particular action, by example
* search, update, delete, etc or a list of them
*
* Example:
* <code>
* //Add a resource to the the list allowing access to an action
* $acl->addResource(new Phalcon\Acl\Resource('customers'), 'search');
* $acl->addResource('customers', 'search');
*
* //Add a resource with an access list
* $acl->addResource(new Phalcon\Acl\Resource('customers'), array('create', 'search'));
* $acl->addResource('customers', array('create', 'search'));
* </code>
*
* @param Phalcon\Acl\Resource $resource
* @param array $accessList
* @return boolean
*/
PHP_METHOD(Phalcon_Acl_Adapter_Memory, addResource){
zval *resource, *access_list = NULL, *resource_name = NULL;
zval *object = NULL, *resources_names;
PHALCON_MM_GROW();
phalcon_fetch_params(1, 1, 1, &resource, &access_list);
if (!access_list) {
access_list = PHALCON_GLOBAL(z_null);
}
if (Z_TYPE_P(resource) == IS_OBJECT) {
PHALCON_INIT_VAR(resource_name);
phalcon_call_method(resource_name, resource, "getname");
PHALCON_CPY_WRT(object, resource);
} else {
PHALCON_CPY_WRT(resource_name, resource);
PHALCON_INIT_NVAR(object);
object_init_ex(object, phalcon_acl_resource_ce);
phalcon_call_method_p1_noret(object, "__construct", resource_name);
}
PHALCON_OBS_VAR(resources_names);
phalcon_read_property_this(&resources_names, this_ptr, SL("_resourcesNames"), PH_NOISY_CC);
if (!phalcon_array_isset(resources_names, resource_name)) {
phalcon_update_property_array_append(this_ptr, SL("_resources"), object TSRMLS_CC);
phalcon_update_property_array(this_ptr, SL("_resourcesNames"), resource_name, PHALCON_GLOBAL(z_true) TSRMLS_CC);
}
phalcon_call_method_p2(return_value, this_ptr, "addresourceaccess", resource_name, access_list);
RETURN_MM();
}
/**
* Adds access to resources
*
* @param string $resourceName
* @param mixed $accessList
*/
PHP_METHOD(Phalcon_Acl_Adapter_Memory, addResourceAccess){
zval *resource_name, *access_list, *resources_names;
zval *exception_message, *internal_access_list;
zval *access_name = NULL, *access_key = NULL;
HashTable *ah0;
HashPosition hp0;
zval **hd;
PHALCON_MM_GROW();
phalcon_fetch_params(1, 2, 0, &resource_name, &access_list);
PHALCON_OBS_VAR(resources_names);
phalcon_read_property_this(&resources_names, this_ptr, SL("_resourcesNames"), PH_NOISY_CC);
if (!phalcon_array_isset(resources_names, resource_name)) {
PHALCON_INIT_VAR(exception_message);
PHALCON_CONCAT_SVS(exception_message, "Resource '", resource_name, "' does not exist in ACL");
PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message);
return;
}
PHALCON_OBS_VAR(internal_access_list);
phalcon_read_property_this(&internal_access_list, this_ptr, SL("_accessList"), PH_NOISY_CC);
if (Z_TYPE_P(access_list) == IS_ARRAY) {
phalcon_is_iterable(access_list, &ah0, &hp0, 0, 0);
while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
PHALCON_GET_HVALUE(access_name);
PHALCON_INIT_NVAR(access_key);
PHALCON_CONCAT_VSV(access_key, resource_name, "!", access_name);
if (!phalcon_array_isset(internal_access_list, access_key)) {
phalcon_update_property_array(this_ptr, SL("_accessList"), access_key, PHALCON_GLOBAL(z_true) TSRMLS_CC);
}
zend_hash_move_forward_ex(ah0, &hp0);
}
} else {
if (Z_TYPE_P(access_list) == IS_STRING) {
PHALCON_INIT_NVAR(access_key);
PHALCON_CONCAT_VSV(access_key, resource_name, "!", access_list);
if (!phalcon_array_isset(internal_access_list, access_key)) {
phalcon_update_property_array(this_ptr, SL("_accessList"), access_key, PHALCON_GLOBAL(z_true) TSRMLS_CC);
}
}
}
RETURN_MM_TRUE;
}
/**
* Removes an access from a resource
*
* @param string $resourceName
* @param mixed $accessList
*/
PHP_METHOD(Phalcon_Acl_Adapter_Memory, dropResourceAccess){
zval *resource_name, *access_list, *access_name = NULL;
zval *access_key = NULL;
HashTable *ah0;
HashPosition hp0;
zval **hd;
PHALCON_MM_GROW();
phalcon_fetch_params(1, 2, 0, &resource_name, &access_list);
if (Z_TYPE_P(access_list) == IS_ARRAY) {
phalcon_is_iterable(access_list, &ah0, &hp0, 0, 0);
while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
PHALCON_GET_HVALUE(access_name);
PHALCON_INIT_NVAR(access_key);
PHALCON_CONCAT_VSV(access_key, resource_name, "!", access_name);
phalcon_unset_property_array(this_ptr, SL("_accessList"), access_key TSRMLS_CC);
zend_hash_move_forward_ex(ah0, &hp0);
}
} else {
PHALCON_INIT_VAR(access_key);
PHALCON_CONCAT_VSV(access_key, resource_name, "!", access_list);
phalcon_unset_property_array(this_ptr, SL("_accessList"), access_key TSRMLS_CC);
}
PHALCON_MM_RESTORE();
}
/**
* Checks if a role has access to a resource
*
* @param string $roleName
* @param string $resourceName
* @param string $access
* @param string $action
*/
PHP_METHOD(Phalcon_Acl_Adapter_Memory, _allowOrDeny){
zval *role_name, *resource_name, *access, *action;
zval *roles_names, *exception_message = NULL, *resources_names;
zval *default_access, *access_list, *internal_access;
zval *access_name = NULL, *access_key = NULL, *access_key_all = NULL;
HashTable *ah0, *ah1;
HashPosition hp0, hp1;
zval **hd;
PHALCON_MM_GROW();
phalcon_fetch_params(1, 4, 0, &role_name, &resource_name, &access, &action);
PHALCON_OBS_VAR(roles_names);
phalcon_read_property_this(&roles_names, this_ptr, SL("_rolesNames"), PH_NOISY_CC);
if (!phalcon_array_isset(roles_names, role_name)) {
PHALCON_INIT_VAR(exception_message);
PHALCON_CONCAT_SVS(exception_message, "Role \"", role_name, "\" does not exist in ACL");
PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message);
return;
}
PHALCON_OBS_VAR(resources_names);
phalcon_read_property_this(&resources_names, this_ptr, SL("_resourcesNames"), PH_NOISY_CC);
if (!phalcon_array_isset(resources_names, resource_name)) {
PHALCON_INIT_NVAR(exception_message);
PHALCON_CONCAT_SVS(exception_message, "Resource \"", resource_name, "\" does not exist in ACL");
PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message);
return;
}
PHALCON_OBS_VAR(default_access);
phalcon_read_property_this(&default_access, this_ptr, SL("_defaultAccess"), PH_NOISY_CC);
PHALCON_OBS_VAR(access_list);
phalcon_read_property_this(&access_list, this_ptr, SL("_accessList"), PH_NOISY_CC);
PHALCON_OBS_VAR(internal_access);
phalcon_read_property_this(&internal_access, this_ptr, SL("_access"), PH_NOISY_CC);
if (Z_TYPE_P(access) == IS_ARRAY) {
phalcon_is_iterable(access, &ah0, &hp0, 0, 0);
while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
PHALCON_GET_HVALUE(access_name);
PHALCON_INIT_NVAR(access_key);
PHALCON_CONCAT_VSV(access_key, resource_name, "!", access_name);
if (!phalcon_array_isset(access_list, access_key)) {
PHALCON_INIT_NVAR(exception_message);
PHALCON_CONCAT_SVSVS(exception_message, "Acccess '", access_name, "' does not exist in resource '", resource_name, "'");
PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message);
return;
}
zend_hash_move_forward_ex(ah0, &hp0);
}
phalcon_is_iterable(access, &ah1, &hp1, 0, 0);
while (zend_hash_get_current_data_ex(ah1, (void**) &hd, &hp1) == SUCCESS) {
PHALCON_GET_HVALUE(access_name);
PHALCON_INIT_NVAR(access_key);
PHALCON_CONCAT_VSVSV(access_key, role_name, "!", resource_name, "!", access_name);
phalcon_update_property_array(this_ptr, SL("_access"), access_key, action TSRMLS_CC);
if (!PHALCON_IS_STRING(access_name, "*")) {
PHALCON_INIT_NVAR(access_key_all);
PHALCON_CONCAT_VSVS(access_key_all, role_name, "!", resource_name, "!*");
if (!phalcon_array_isset(internal_access, access_key_all)) {
phalcon_update_property_array(this_ptr, SL("_access"), access_key_all, default_access TSRMLS_CC);
}
}
zend_hash_move_forward_ex(ah1, &hp1);
}
} else {
if (!PHALCON_IS_STRING(access, "*")) {
PHALCON_INIT_NVAR(access_key);
PHALCON_CONCAT_VSV(access_key, resource_name, "!", access);
if (!phalcon_array_isset(access_list, access_key)) {
PHALCON_INIT_NVAR(exception_message);
PHALCON_CONCAT_SVSVS(exception_message, "Acccess '", access, "' does not exist in resource '", resource_name, "'");
PHALCON_THROW_EXCEPTION_ZVAL(phalcon_acl_exception_ce, exception_message);
return;
}
}
PHALCON_INIT_NVAR(access_key);
PHALCON_CONCAT_VSVSV(access_key, role_name, "!", resource_name, "!", access);
/**
* Define the access action for the specified accessKey
*/
phalcon_update_property_array(this_ptr, SL("_access"), access_key, action TSRMLS_CC);
if (!PHALCON_IS_STRING(access, "*")) {
PHALCON_INIT_NVAR(access_key);
PHALCON_CONCAT_VSVS(access_key, role_name, "!", resource_name, "!*");
/**
* If there is no default action for all the rest actions in the resource set the
* default one
*/
if (!phalcon_array_isset(internal_access, access_key)) {
phalcon_update_property_array(this_ptr, SL("_access"), access_key, default_access TSRMLS_CC);
}
}
}
PHALCON_MM_RESTORE();
}
/**
* Allow access to a role on a resource
*
* You can use '*' as wildcard
*
* Example:
* <code>
* //Allow access to guests to search on customers
* $acl->allow('guests', 'customers', 'search');
*
* //Allow access to guests to search or create on customers
* $acl->allow('guests', 'customers', array('search', 'create'));
*
* //Allow access to any role to browse on products
* $acl->allow('*', 'products', 'browse');
*
* //Allow access to any role to browse on any resource
* $acl->allow('*', '*', 'browse');
* </code>
*
* @param string $roleName
* @param string $resourceName
* @param mixed $access
*/
PHP_METHOD(Phalcon_Acl_Adapter_Memory, allow){
zval *role_name, *resource_name, *access, *action, *roles_names;
HashTable *ah0;
HashPosition hp0;
zval **hd;
PHALCON_MM_GROW();
phalcon_fetch_params(1, 3, 0, &role_name, &resource_name, &access);
PHALCON_INIT_VAR(action);
ZVAL_LONG(action, 1);
if (!PHALCON_IS_STRING(role_name, "*")) {
phalcon_call_method_p4(return_value, this_ptr, "_allowordeny", role_name, resource_name, access, action);
} else {
PHALCON_SEPARATE_PARAM(role_name);
PHALCON_OBS_VAR(roles_names);
phalcon_read_property_this(&roles_names, this_ptr, SL("_rolesNames"), PH_NOISY_CC);
phalcon_is_iterable(roles_names, &ah0, &hp0, 0, 0);
while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
PHALCON_GET_HKEY(role_name, ah0, hp0);
phalcon_call_method_p4_noret(this_ptr, "_allowordeny", role_name, resource_name, access, action);
zend_hash_move_forward_ex(ah0, &hp0);
}
}
RETURN_MM();
}
/**
* Deny access to a role on a resource
*
* You can use '*' as wildcard
*
* Example:
* <code>
* //Deny access to guests to search on customers
* $acl->deny('guests', 'customers', 'search');
*
* //Deny access to guests to search or create on customers
* $acl->deny('guests', 'customers', array('search', 'create'));
*
* //Deny access to any role to browse on products
* $acl->deny('*', 'products', 'browse');
*
* //Deny access to any role to browse on any resource
* $acl->deny('*', '*', 'browse');
* </code>
*
* @param string $roleName
* @param string $resourceName
* @param mixed $access
* @return boolean
*/
PHP_METHOD(Phalcon_Acl_Adapter_Memory, deny){
zval *role_name, *resource_name, *access, *action, *roles_names;
HashTable *ah0;
HashPosition hp0;
zval **hd;
PHALCON_MM_GROW();
phalcon_fetch_params(1, 3, 0, &role_name, &resource_name, &access);
PHALCON_INIT_VAR(action);
ZVAL_LONG(action, 0);
if (!PHALCON_IS_STRING(role_name, "*")) {
phalcon_call_method_p4(return_value, this_ptr, "_allowordeny", role_name, resource_name, access, action);
} else {
PHALCON_SEPARATE_PARAM(role_name);
PHALCON_OBS_VAR(roles_names);
phalcon_read_property_this(&roles_names, this_ptr, SL("_rolesNames"), PH_NOISY_CC);
phalcon_is_iterable(roles_names, &ah0, &hp0, 0, 0);
while (zend_hash_get_current_data_ex(ah0, (void**) &hd, &hp0) == SUCCESS) {
PHALCON_GET_HKEY(role_name, ah0, hp0);
phalcon_call_method_p4_noret(this_ptr, "_allowordeny", role_name, resource_name, access, action);
zend_hash_move_forward_ex(ah0, &hp0);
}
}
RETURN_MM();
}
#define PHALCON_ACL_NO 0
#define PHALCON_ACL_YES 1
#define PHALCON_ACL_DUNNO -1
static int phalcon_role_adapter_memory_check_inheritance(zval *role, zval *resource, zval *access, zval *access_list, zval* role_inherits TSRMLS_DC)
{
zval *inherited_roles, *access_key;
zval **parent_role;
int result = PHALCON_ACL_DUNNO;
HashPosition hp;
assert(Z_TYPE_P(role) == IS_STRING);
assert(Z_TYPE_P(resource) == IS_STRING);
assert(Z_TYPE_P(access) == IS_STRING);
assert(Z_TYPE_P(access_list) == IS_ARRAY);
if (!phalcon_array_isset_fetch(&inherited_roles, role_inherits, role) || Z_TYPE_P(inherited_roles) != IS_ARRAY) {
return PHALCON_ACL_DUNNO;
}
ALLOC_INIT_ZVAL(access_key);
for (
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(inherited_roles), &hp);
zend_hash_get_current_data_ex(Z_ARRVAL_P(inherited_roles), (void**)&parent_role, &hp) == SUCCESS;
zend_hash_move_forward_ex(Z_ARRVAL_P(inherited_roles), &hp)
) {
int found;
zval *have_access;
phalcon_concat_vsvsv(&access_key, *parent_role, SL("!"), resource, SL("!"), access, 0 TSRMLS_CC);
found = phalcon_array_isset_fetch(&have_access, access_list, access_key);
zval_dtor(access_key);
ZVAL_NULL(access_key);
if (found) {
result = zend_is_true(have_access) ? PHALCON_ACL_YES : PHALCON_ACL_NO;
}
else {
result = phalcon_role_adapter_memory_check_inheritance(*parent_role, resource, access, access_list, role_inherits TSRMLS_CC);
}
if (PHALCON_ACL_DUNNO != result) {
break;
}
}
zval_ptr_dtor(&access_key);
return result;
}
/**
* Check whether a role is allowed to access an action from a resource
*
* <code>
* //Does andres have access to the customers resource to create?
* $acl->isAllowed('andres', 'Products', 'create');
*
* //Do guests have access to any resource to edit?
* $acl->isAllowed('guests', '*', 'edit');
* </code>
*
* @param string $role
* @param string $resource
* @param string $access
* @return boolean
*/
PHP_METHOD(Phalcon_Acl_Adapter_Memory, isAllowed){
zval *role, *resource, *access, *events_manager, *role_inherits;
zval *event_name = NULL, *status, *default_access, *roles_names;
zval *have_access = NULL, *access_list, *access_key = NULL;
zval star;
int allow_access;
PHALCON_MM_GROW();
INIT_ZVAL(star);
ZVAL_STRING(&star, "*", 0);
phalcon_fetch_params(1, 3, 0, &role, &resource, &access);
phalcon_update_property_this(this_ptr, SL("_activeRole"), role TSRMLS_CC);
phalcon_update_property_this(this_ptr, SL("_activeResource"), resource TSRMLS_CC);
phalcon_update_property_this(this_ptr, SL("_activeAccess"), access TSRMLS_CC);
events_manager = phalcon_fetch_nproperty_this(this_ptr, SL("_eventsManager"), PH_NOISY_CC);
if (Z_TYPE_P(events_manager) == IS_OBJECT) {
PHALCON_INIT_VAR(event_name);
ZVAL_STRING(event_name, "acl:beforeCheckAccess", 1);
PHALCON_INIT_VAR(status);
phalcon_call_method_p2(status, events_manager, "fire", event_name, this_ptr);
if (PHALCON_IS_FALSE(status)) {
RETURN_CTOR(status);
}
}
default_access = phalcon_fetch_nproperty_this(this_ptr, SL("_defaultAccess"), PH_NOISY_CC);
/**
* Check if the role exists
*/
roles_names = phalcon_fetch_nproperty_this(this_ptr, SL("_rolesNames"), PH_NOISY_CC);
if (!phalcon_array_isset(roles_names, role)) {
RETURN_CTOR(default_access);
}
access_list = phalcon_fetch_nproperty_this(this_ptr, SL("_access"), PH_NOISY_CC);
PHALCON_INIT_VAR(access_key);
PHALCON_CONCAT_VSVSV(access_key, role, "!", resource, "!", access);
/**
* Check if there is a direct combination for role-resource-access
*/
if (phalcon_array_isset_fetch(&have_access, access_list, access_key)) {
allow_access = zend_is_true(have_access) ? PHALCON_ACL_YES : PHALCON_ACL_NO;
}
else {
allow_access = PHALCON_ACL_DUNNO;
}
/**
* Check in the inherits roles
*/
if (PHALCON_ACL_DUNNO == allow_access) {
role_inherits = phalcon_fetch_nproperty_this(this_ptr, SL("_roleInherits"), PH_NOISY TSRMLS_CC);
allow_access = phalcon_role_adapter_memory_check_inheritance(role, resource, access, access_list, role_inherits TSRMLS_CC);
}
/**
* If access wasn't found yet, try role-resource-*
*/
if (PHALCON_ACL_DUNNO == allow_access) {
PHALCON_INIT_NVAR(access_key);
PHALCON_CONCAT_VSVS(access_key, role, "!", resource, "!*");
/**
* In the direct role
*/
if (phalcon_array_isset_fetch(&have_access, access_list, access_key)) {
allow_access = zend_is_true(have_access) ? PHALCON_ACL_YES : PHALCON_ACL_NO;
}
else {
allow_access = phalcon_role_adapter_memory_check_inheritance(role, resource, &star, access_list, role_inherits TSRMLS_CC);
}
}
/**
* If access wasn't found yet, try role-*-*
*/
if (PHALCON_ACL_DUNNO == allow_access) {
PHALCON_INIT_NVAR(access_key);
PHALCON_CONCAT_VS(access_key, role, "!*!*");
/**
* Try in the direct role
*/
if (phalcon_array_isset_fetch(&have_access, access_list, access_key)) {
allow_access = zend_is_true(have_access) ? PHALCON_ACL_YES : PHALCON_ACL_NO;
}
else {
allow_access = phalcon_role_adapter_memory_check_inheritance(role, &star, &star, access_list, role_inherits TSRMLS_CC);
}
}
ZVAL_BOOL(return_value, PHALCON_ACL_YES == allow_access);
phalcon_update_property_this(this_ptr, SL("_accessGranted"), return_value TSRMLS_CC);
if (Z_TYPE_P(events_manager) == IS_OBJECT) {
PHALCON_INIT_NVAR(event_name);
ZVAL_STRING(event_name, "acl:afterCheckAccess", 1);
phalcon_call_method_p3_noret(events_manager, "fire", event_name, this_ptr, return_value);
}
PHALCON_MM_RESTORE();
}
/**
* Return an array with every role registered in the list
*
* @return Phalcon\Acl\Role[]
*/
PHP_METHOD(Phalcon_Acl_Adapter_Memory, getRoles){
RETURN_MEMBER(this_ptr, "_roles");
}
/**
* Return an array with every resource registered in the list
*
* @return Phalcon\Acl\Resource[]
*/
PHP_METHOD(Phalcon_Acl_Adapter_Memory, getResources){
RETURN_MEMBER(this_ptr, "_resources");
}