Class FilteredAdapter

  • All Implemented Interfaces:
    Adapter, FilteredAdapter

    public class FilteredAdapter
    extends java.lang.Object
    implements FilteredAdapter
    FilteredAdapter is the filtered file adapter for Casbin. It can load policy from a file or save policy to a file and supports loading of filtered policies.
    Since:
    2020/6/8
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  FilteredAdapter.Filter
      the filter class.
    • Constructor Summary

      Constructors 
      Constructor Description
      FilteredAdapter​(java.lang.String filepath)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addPolicy​(java.lang.String sec, java.lang.String ptype, java.util.List<java.lang.String> rule)
      addPolicy adds a policy rule to the storage.
      private boolean filterLine​(java.lang.String line, FilteredAdapter.Filter filter)
      match the line.
      private boolean filterWords​(java.lang.String[] line, java.lang.String[] filter)
      match the words in the specific line.
      boolean isFiltered()
      IsFiltered returns true if the loaded policy has been filtered.
      void loadFilteredPolicy​(Model model, java.lang.Object filter)
      loadFilteredPolicy loads only policy rules that match the filter.
      private void loadFilteredPolicyFile​(Model model, FilteredAdapter.Filter filter, Helper.loadPolicyLineHandler<java.lang.String,​Model> handler)
      loadFilteredPolicyFile loads only policy rules that match the filter from file.
      void loadPolicy​(Model model)
      loadPolicy loads all policy rules from the storage.
      void removeFilteredPolicy​(java.lang.String sec, java.lang.String ptype, int fieldIndex, java.lang.String... fieldValues)
      removeFilteredPolicy removes policy rules that match the filter from the storage.
      void removePolicy​(java.lang.String sec, java.lang.String ptype, java.util.List<java.lang.String> rule)
      removePolicy removes a policy rule from the storage.
      void savePolicy​(Model model)
      savePolicy saves all policy rules to the storage.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • isFiltered

        private boolean isFiltered
      • filepath

        private java.lang.String filepath
    • Constructor Detail

      • FilteredAdapter

        public FilteredAdapter​(java.lang.String filepath)
    • Method Detail

      • loadFilteredPolicy

        public void loadFilteredPolicy​(Model model,
                                       java.lang.Object filter)
                                throws CasbinAdapterException
        loadFilteredPolicy loads only policy rules that match the filter.
        Specified by:
        loadFilteredPolicy in interface FilteredAdapter
        Parameters:
        model - the model.
        filter - the filter used to specify which type of policy should be loaded.
        Throws:
        CasbinAdapterException - if the file path or the type of the filter is incorrect.
      • filterLine

        private boolean filterLine​(java.lang.String line,
                                   FilteredAdapter.Filter filter)
        match the line.
      • filterWords

        private boolean filterWords​(java.lang.String[] line,
                                    java.lang.String[] filter)
        match the words in the specific line.
      • isFiltered

        public boolean isFiltered()
        Description copied from interface: FilteredAdapter
        IsFiltered returns true if the loaded policy has been filtered.
        Specified by:
        isFiltered in interface FilteredAdapter
        Returns:
        true if have any filter roles.
      • loadPolicy

        public void loadPolicy​(Model model)
        loadPolicy loads all policy rules from the storage.
        Specified by:
        loadPolicy in interface Adapter
        Parameters:
        model - the model.
      • savePolicy

        public void savePolicy​(Model model)
        savePolicy saves all policy rules to the storage.
        Specified by:
        savePolicy in interface Adapter
        Parameters:
        model - the model.
      • addPolicy

        public void addPolicy​(java.lang.String sec,
                              java.lang.String ptype,
                              java.util.List<java.lang.String> rule)
        addPolicy adds a policy rule to the storage.
        Specified by:
        addPolicy in interface Adapter
        Parameters:
        sec - the section, "p" or "g".
        ptype - the policy type, "p", "p2", .. or "g", "g2", ..
        rule - the rule, like (sub, obj, act).
      • removePolicy

        public void removePolicy​(java.lang.String sec,
                                 java.lang.String ptype,
                                 java.util.List<java.lang.String> rule)
        removePolicy removes a policy rule from the storage.
        Specified by:
        removePolicy in interface Adapter
        Parameters:
        sec - the section, "p" or "g".
        ptype - the policy type, "p", "p2", .. or "g", "g2", ..
        rule - the rule, like (sub, obj, act).
      • removeFilteredPolicy

        public void removeFilteredPolicy​(java.lang.String sec,
                                         java.lang.String ptype,
                                         int fieldIndex,
                                         java.lang.String... fieldValues)
        removeFilteredPolicy removes policy rules that match the filter from the storage.
        Specified by:
        removeFilteredPolicy in interface Adapter
        Parameters:
        sec - the section, "p" or "g".
        ptype - the policy type, "p", "p2", .. or "g", "g2", ..
        fieldIndex - the policy rule's start index to be matched.
        fieldValues - the field values to be matched, value "" means not to match this field.