@@ -108,17 +108,18 @@ pub fn access_controllable(attrs: TokenStream, item: TokenStream) -> TokenStream
108
108
} )
109
109
}
110
110
111
- fn acl_get ( & self ) -> #acl_type {
112
- self . acl_get_storage( ) . unwrap_or_else( || :: near_sdk :: env :: panic_str ( "ACL: storage isn't initialized" ) )
111
+ fn acl_get_or_init ( & self ) -> #acl_type {
112
+ self . acl_get_storage( ) . unwrap_or_else( || self . acl_init_storage_unchecked ( ) )
113
113
}
114
114
115
- fn acl_init_storage_unchecked( & self ) {
115
+ fn acl_init_storage_unchecked( & self ) -> #acl_type {
116
116
let base_prefix = <#ident as AccessControllable >:: acl_storage_prefix( ) ;
117
117
let acl_storage: #acl_type = Default :: default ( ) ;
118
118
near_sdk:: env:: storage_write(
119
119
& __acl_storage_prefix( base_prefix, __AclStorageKey:: AclStorage ) ,
120
120
& acl_storage. try_to_vec( ) . unwrap( ) ,
121
121
) ;
122
+ acl_storage
122
123
}
123
124
}
124
125
@@ -496,6 +497,39 @@ pub fn access_controllable(attrs: TokenStream, item: TokenStream) -> TokenStream
496
497
}
497
498
}
498
499
500
+ fn get_default_permissioned_accounts( ) -> #cratename:: access_controllable:: PermissionedAccounts {
501
+ let roles = <#role_type>:: acl_role_variants( ) ;
502
+ let mut map = :: std:: collections:: HashMap :: new( ) ;
503
+ for role in roles {
504
+ let role: #role_type = :: std:: convert:: TryFrom :: try_from( role)
505
+ . unwrap_or_else( |_| :: near_sdk:: env:: panic_str( #ERR_PARSE_ROLE ) ) ;
506
+
507
+ map. insert(
508
+ role. into( ) ,
509
+ #cratename:: access_controllable:: PermissionedAccountsPerRole {
510
+ admins: Default :: default ( ) ,
511
+ grantees: Default :: default ( ) ,
512
+ }
513
+ ) ;
514
+ }
515
+
516
+ #cratename:: access_controllable:: PermissionedAccounts {
517
+ super_admins: Default :: default ( ) ,
518
+ roles: map,
519
+ }
520
+ }
521
+
522
+ macro_rules! return_if_none {
523
+ ( $res: expr, $default_value: expr) => {
524
+ match $res {
525
+ Some ( val) => val,
526
+ None => {
527
+ return $default_value;
528
+ }
529
+ }
530
+ } ;
531
+ }
532
+
499
533
// Note that `#[near-bindgen]` exposes non-public functions in trait
500
534
// implementations. This is [documented] behavior. Therefore some
501
535
// functions are made `#[private]` despite _not_ being public.
@@ -507,63 +541,58 @@ pub fn access_controllable(attrs: TokenStream, item: TokenStream) -> TokenStream
507
541
( #storage_prefix) . as_bytes( )
508
542
}
509
543
510
- fn acl_init_storage( & self ) {
511
- :: near_sdk:: require!( self . acl_get_storage( ) . is_none( ) , "ACL: storage was already initialized" ) ;
512
- self . acl_init_storage_unchecked( ) ;
513
- }
514
-
515
544
#[ private]
516
545
fn acl_init_super_admin( & mut self , account_id: :: near_sdk:: AccountId ) -> bool {
517
- self . acl_get ( ) . init_super_admin( & account_id)
546
+ self . acl_get_or_init ( ) . init_super_admin( & account_id)
518
547
}
519
548
520
549
fn acl_role_variants( & self ) -> Vec <& ' static str > {
521
550
<#role_type>:: acl_role_variants( )
522
551
}
523
552
524
553
fn acl_is_super_admin( & self , account_id: :: near_sdk:: AccountId ) -> bool {
525
- self . acl_get ( ) . is_super_admin( & account_id)
554
+ return_if_none! ( self . acl_get_storage ( ) , false ) . is_super_admin( & account_id)
526
555
}
527
556
528
557
fn acl_add_admin( & mut self , role: String , account_id: :: near_sdk:: AccountId ) -> Option <bool > {
529
558
let role: #role_type = :: std:: convert:: TryFrom :: try_from( role. as_str( ) ) . unwrap_or_else( |_| :: near_sdk:: env:: panic_str( #ERR_PARSE_ROLE ) ) ;
530
- self . acl_get ( ) . add_admin( role, & account_id)
559
+ self . acl_get_or_init ( ) . add_admin( role, & account_id)
531
560
}
532
561
533
562
fn acl_is_admin( & self , role: String , account_id: :: near_sdk:: AccountId ) -> bool {
534
563
let role: #role_type = :: std:: convert:: TryFrom :: try_from( role. as_str( ) ) . unwrap_or_else( |_| :: near_sdk:: env:: panic_str( #ERR_PARSE_ROLE ) ) ;
535
- self . acl_get ( ) . is_admin( role, & account_id)
564
+ return_if_none! ( self . acl_get_storage ( ) , false ) . is_admin( role, & account_id)
536
565
}
537
566
538
567
fn acl_revoke_admin( & mut self , role: String , account_id: :: near_sdk:: AccountId ) -> Option <bool > {
539
568
let role: #role_type = :: std:: convert:: TryFrom :: try_from( role. as_str( ) ) . unwrap_or_else( |_| :: near_sdk:: env:: panic_str( #ERR_PARSE_ROLE ) ) ;
540
- self . acl_get ( ) . revoke_admin( role, & account_id)
569
+ self . acl_get_or_init ( ) . revoke_admin( role, & account_id)
541
570
}
542
571
543
572
fn acl_renounce_admin( & mut self , role: String ) -> bool {
544
573
let role: #role_type = :: std:: convert:: TryFrom :: try_from( role. as_str( ) ) . unwrap_or_else( |_| :: near_sdk:: env:: panic_str( #ERR_PARSE_ROLE ) ) ;
545
- self . acl_get ( ) . renounce_admin( role)
574
+ self . acl_get_or_init ( ) . renounce_admin( role)
546
575
}
547
576
548
577
fn acl_revoke_role( & mut self , role: String , account_id: :: near_sdk:: AccountId ) -> Option <bool > {
549
578
let role: #role_type = :: std:: convert:: TryFrom :: try_from( role. as_str( ) ) . unwrap_or_else( |_| :: near_sdk:: env:: panic_str( #ERR_PARSE_ROLE ) ) ;
550
- self . acl_get ( ) . revoke_role( role, & account_id)
579
+ self . acl_get_or_init ( ) . revoke_role( role, & account_id)
551
580
}
552
581
553
582
fn acl_renounce_role( & mut self , role: String ) -> bool {
554
583
let role: #role_type = :: std:: convert:: TryFrom :: try_from( role. as_str( ) ) . unwrap_or_else( |_| :: near_sdk:: env:: panic_str( #ERR_PARSE_ROLE ) ) ;
555
- self . acl_get ( ) . renounce_role( role)
584
+ self . acl_get_or_init ( ) . renounce_role( role)
556
585
}
557
586
558
587
fn acl_grant_role( & mut self , role: String , account_id: :: near_sdk:: AccountId ) -> Option <bool > {
559
588
let role: #role_type = :: std:: convert:: TryFrom :: try_from( role. as_str( ) ) . unwrap_or_else( |_| :: near_sdk:: env:: panic_str( #ERR_PARSE_ROLE ) ) ;
560
- self . acl_get ( ) . grant_role( role, & account_id)
589
+ self . acl_get_or_init ( ) . grant_role( role, & account_id)
561
590
}
562
591
563
592
564
593
fn acl_has_role( & self , role: String , account_id: :: near_sdk:: AccountId ) -> bool {
565
594
let role: #role_type = :: std:: convert:: TryFrom :: try_from( role. as_str( ) ) . unwrap_or_else( |_| :: near_sdk:: env:: panic_str( #ERR_PARSE_ROLE ) ) ;
566
- self . acl_get ( ) . has_role( role, & account_id)
595
+ return_if_none! ( self . acl_get_storage ( ) , false ) . has_role( role, & account_id)
567
596
}
568
597
569
598
fn acl_has_any_role( & self , roles: Vec <String >, account_id: :: near_sdk:: AccountId ) -> bool {
@@ -573,33 +602,33 @@ pub fn access_controllable(attrs: TokenStream, item: TokenStream) -> TokenStream
573
602
:: std:: convert:: TryFrom :: try_from( role. as_str( ) ) . unwrap_or_else( |_| :: near_sdk:: env:: panic_str( #ERR_PARSE_ROLE ) )
574
603
} )
575
604
. collect( ) ;
576
- self . acl_get ( ) . has_any_role( roles, & account_id)
605
+ return_if_none! ( self . acl_get_storage ( ) , false ) . has_any_role( roles, & account_id)
577
606
}
578
607
579
608
fn acl_get_super_admins( & self , skip: u64 , limit: u64 ) -> Vec <:: near_sdk:: AccountId > {
580
609
let permission = <#bitflags_type>:: from_bits(
581
610
<#role_type>:: acl_super_admin_permission( )
582
611
)
583
612
. unwrap_or_else( || :: near_sdk:: env:: panic_str( #ERR_PARSE_BITFLAG ) ) ;
584
- self . acl_get ( ) . get_bearers( permission, skip, limit)
613
+ return_if_none! ( self . acl_get_storage ( ) , vec! [ ] ) . get_bearers( permission, skip, limit)
585
614
}
586
615
587
616
fn acl_get_admins( & self , role: String , skip: u64 , limit: u64 ) -> Vec <:: near_sdk:: AccountId > {
588
617
let role: #role_type = :: std:: convert:: TryFrom :: try_from( role. as_str( ) ) . unwrap_or_else( |_| :: near_sdk:: env:: panic_str( #ERR_PARSE_ROLE ) ) ;
589
618
let permission = <#bitflags_type>:: from_bits( role. acl_admin_permission( ) )
590
619
. unwrap_or_else( || :: near_sdk:: env:: panic_str( #ERR_PARSE_BITFLAG ) ) ;
591
- self . acl_get ( ) . get_bearers( permission, skip, limit)
620
+ return_if_none! ( self . acl_get_storage ( ) , vec! [ ] ) . get_bearers( permission, skip, limit)
592
621
}
593
622
594
623
fn acl_get_grantees( & self , role: String , skip: u64 , limit: u64 ) -> Vec <:: near_sdk:: AccountId > {
595
624
let role: #role_type = :: std:: convert:: TryFrom :: try_from( role. as_str( ) ) . unwrap_or_else( |_| :: near_sdk:: env:: panic_str( #ERR_PARSE_ROLE ) ) ;
596
625
let permission = <#bitflags_type>:: from_bits( role. acl_permission( ) )
597
626
. unwrap_or_else( || :: near_sdk:: env:: panic_str( #ERR_PARSE_BITFLAG ) ) ;
598
- self . acl_get ( ) . get_bearers( permission, skip, limit)
627
+ return_if_none! ( self . acl_get_storage ( ) , vec! [ ] ) . get_bearers( permission, skip, limit)
599
628
}
600
629
601
630
fn acl_get_permissioned_accounts( & self ) -> #cratename:: access_controllable:: PermissionedAccounts {
602
- self . acl_get ( ) . get_permissioned_accounts( )
631
+ return_if_none! ( self . acl_get_storage ( ) , get_default_permissioned_accounts ( ) ) . get_permissioned_accounts( )
603
632
}
604
633
}
605
634
} ;
0 commit comments