| 814 | } |
| 815 | |
| 816 | pub fn set_initial_named_rules(named_rules: &[InitNamedRuleInput]) -> Result<(), ApiError> { |
| 817 | let mut add_named_rules = named_rules |
| 818 | .iter() |
| 819 | .map(|named_rule| { |
| 820 | let input = AddNamedRuleOperationInput { |
| 821 | name: named_rule.name.clone(), |
| 822 | description: named_rule.description.clone(), |
| 823 | rule: named_rule.rule.clone().into(), |
| 824 | }; |
| 825 | |
| 826 | let named_rule_id = named_rule |
| 827 | .id |
| 828 | .as_ref() |
| 829 | .map(|id| HelperMapper::to_uuid(id.clone()).map(|uuid| *uuid.as_bytes())) |
| 830 | .transpose(); |
| 831 | |
| 832 | named_rule_id.map(|named_rule_id| (input, named_rule_id)) |
| 833 | }) |
| 834 | .collect::<Result<Vec<_>, _>>()?; |
| 835 | |
| 836 | // sorting criteria: |
| 837 | // - if a policy depends on another policy, the dependent policy should be added first |
| 838 | // - keep the original order of the policies otherwise |
| 839 | add_named_rules.sort_by(|a, b| { |
| 840 | if let Some(a_id) = &a.1 { |
| 841 | if b.0.rule.has_named_rule_id(a_id) { |
| 842 | return Ordering::Less; |
| 843 | } |
| 844 | } |
| 845 | Ordering::Equal |
| 846 | }); |
| 847 | |
| 848 | for (new_named_rule, with_named_rule_id) in add_named_rules { |
| 849 | NAMED_RULE_SERVICE.create_with_id(new_named_rule, with_named_rule_id)?; |
| 850 | } |
| 851 | |
| 852 | Ok(()) |
| 853 | } |
| 854 | |
| 855 | pub async fn set_initial_permissions( |
| 856 | permissions: &[InitPermissionInput], |