Commits

Taylor Venable committed 06b8873

Allow attr_val rule to have multiple attributes.

Comments (0)

Files changed (1)

lib/Local/Rule/Attr_Value.pm

 sub validate {
     my $rule = shift;
 
+    if (not exists $$rule{'attribute'} || exists $$rule{'attributes'}) {
+        croak qq|rule <$$rule{'index'}> invalid: missing 'attribute' or 'attributes'|;
+    }
 }
 
 sub optimize {
     }
     $$rule{'match_style'} //= 'exact';
     $$rule{'scope'} //= 'matching';
+    $$rule{'attributes'} //= [$$rule{'attribute'}];
 
     if ($$rule{'match_style'} eq 'regexp') {
         my @regexps = map { qr/$_/ } @{$$rule{'values'}};
     my $entry = shift;
     my $rule = shift;
 
-    my $attr = $$rule{'attribute'};
-
-    my @values = $entry->get_value($$rule{'attribute'}) or return 0;
-  VALUE:
-    foreach my $value (@values) {
-        my $condition = $$rule{'values'}->member($value);
-        $condition = not $condition if $$rule{'invert'};
-        next VALUE if not $condition;
-        given ($$rule{'target'}) {
-            when ('ATTRIBUTE') {
-                given ($$rule{'action'}) {
-                    when ('DROP') {
-                        if (not main::is_attr_accepted($entry, $attr)) {
-                            given ($$rule{'scope'}) {
-                                when ('all') {
-                                    $main::LOG{'attr_value'}->debug(
-                                        qq|<$$rule{'index'}> DROP ATTRIBUTE ALL $attr |,
-                                        $entry->dn
-                                    );
-                                    $main::STATS{'drop_attr'} += 1;
-                                    $entry->delete($attr => []);
-                                }
-                                when ('matching') {
-                                    $main::LOG{'attr_value'}->debug(
-                                        qq|<$$rule{'index'}> DROP ATTRIBUTE VALUE $attr "$value" |,
-                                        $entry->dn
-                                    );
-                                    $main::STATS{'drop_attr'} += 1;
-                                    $entry->delete($attr => [$value]);
-                                }
-                                default {
-                                    croak qq|invalid: rule "$$rule{'name'}", scope /= {all, matching}|;
+  ATTRIBUTE:
+    foreach my $attr (@{$$rule{'attributes'}}) {
+        my @values = $entry->get_value($attr) or next ATTRIBUTE;
+      VALUE:
+        foreach my $value (@values) {
+            my $condition = $$rule{'values'}->member($value);
+            $condition = not $condition if $$rule{'invert'};
+            next VALUE if not $condition;
+            given ($$rule{'target'}) {
+                when ('ATTRIBUTE') {
+                    given ($$rule{'action'}) {
+                        when ('DROP') {
+                            if (not main::is_attr_accepted($entry, $attr)) {
+                                given ($$rule{'scope'}) {
+                                    when ('all') {
+                                        $main::LOG{'attr_value'}->debug(
+                                            qq|<$$rule{'index'}> DROP ATTRIBUTE ALL $attr |,
+                                            $entry->dn
+                                        );
+                                        $main::STATS{'drop_attr'} += 1;
+                                        $entry->delete($attr => []);
+                                    }
+                                    when ('matching') {
+                                        $main::LOG{'attr_value'}->debug(
+                                            qq|<$$rule{'index'}> DROP ATTRIBUTE VALUE $attr "$value" |,
+                                            $entry->dn
+                                        );
+                                        $main::STATS{'drop_attr'} += 1;
+                                        $entry->delete($attr => [$value]);
+                                    }
+                                    default {
+                                        croak qq|invalid: rule "$$rule{'name'}", scope /= {all, matching}|;
+                                    }
                                 }
                             }
                         }
-                    }
-                    when ('ACCEPT QUICK') {
-                        main::mark_attr_accepted($entry, $attr);
-                    }
-                    default {
-                        croak qq|non-sense: rule "$$rule{'name'}", target = ATTRIBUTE, action /= {DROP, ACCEPT QUICK}|;
+                        when ('ACCEPT QUICK') {
+                            main::mark_attr_accepted($entry, $attr);
+                        }
+                        default {
+                            croak qq|non-sense: rule "$$rule{'name'}", target = ATTRIBUTE, action /= {DROP, ACCEPT QUICK}|;
+                        }
                     }
                 }
-            }
-            when ('ENTRY') {
-                $main::LOG{'attr_value'}->debug("<$$rule{'index'}> MATCH ENTRY $attr ", $entry->dn);
-                return 1;
+                when ('ENTRY') {
+                    $main::LOG{'attr_value'}->debug("<$$rule{'index'}> MATCH ENTRY $attr ", $entry->dn);
+                    return 1;
+                }
             }
         }
     }
     my $entry = shift;
     my $rule = shift;
 
-    my $attr = $$rule{'attribute'};
-
-    my @values = $entry->get_value($$rule{'attribute'}) or return 0;
-    foreach my $value (@values) {
-      REGEXP:
-        foreach my $regexp (@{$$rule{'values'}}) {
-            if ($value =~ $regexp) {
-                given ($$rule{'target'}) {
-                    when ('ATTRIBUTE') {
-                        given ($$rule{'action'}) {
-                            when ('DROP') {
-                                if (not main::is_attr_accepted($entry, $attr)) {
-                                    given ($$rule{'scope'}) {
-                                        when ('all') {
-                                            $main::LOG{'attr_value'}->debug(
-                                                qq|<$$rule{'index'}> DROP ATTRIBUTE ALL $attr |,
-                                                $entry->dn
-                                            );
-                                            $main::STATS{'drop_attr'} += 1;
-                                            $entry->delete($attr => []);
-                                        }
-                                        when ('matching') {
-                                            $main::LOG{'attr_value'}->debug(
-                                                qq|<$$rule{'index'}> DROP ATTRIBUTE VALUE $attr "$value" |,
-                                                $entry->dn
-                                            );
-                                            $main::STATS{'drop_attr'} += 1;
-                                            $entry->delete($attr => [$value]);
-                                        }
-                                        default {
-                                            croak qq|invalid: rule "$$rule{'name'}", scope /= {all, matching}|;
+  ATTRIBUTE:
+    foreach my $attr (@{$$rule{'attributes'}}) {
+        my @values = $entry->get_value($attr) or next ATTRIBUTE;
+        foreach my $value (@values) {
+          REGEXP:
+            foreach my $regexp (@{$$rule{'values'}}) {
+                if ($value =~ $regexp) {
+                    given ($$rule{'target'}) {
+                        when ('ATTRIBUTE') {
+                            given ($$rule{'action'}) {
+                                when ('DROP') {
+                                    if (not main::is_attr_accepted($entry, $attr)) {
+                                        given ($$rule{'scope'}) {
+                                            when ('all') {
+                                                $main::LOG{'attr_value'}->debug(
+                                                    qq|<$$rule{'index'}> DROP ATTRIBUTE ALL $attr |,
+                                                    $entry->dn
+                                                );
+                                                $main::STATS{'drop_attr'} += 1;
+                                                $entry->delete($attr => []);
+                                            }
+                                            when ('matching') {
+                                                $main::LOG{'attr_value'}->debug(
+                                                    qq|<$$rule{'index'}> DROP ATTRIBUTE VALUE $attr "$value" |,
+                                                    $entry->dn
+                                                );
+                                                $main::STATS{'drop_attr'} += 1;
+                                                $entry->delete($attr => [$value]);
+                                            }
+                                            default {
+                                                croak qq|invalid: rule "$$rule{'name'}", scope /= {all, matching}|;
+                                            }
                                         }
                                     }
                                 }
-                            }
-                            when ('ACCEPT QUICK') {
-                                main::mark_attr_accepted($entry, $attr);
-                            }
-                            default {
-                                croak qq|non-sense: rule "$$rule{'name'}", target = ATTRIBUTE, action /= {DROP, ACCEPT QUICK}|;
+                                when ('ACCEPT QUICK') {
+                                    main::mark_attr_accepted($entry, $attr);
+                                }
+                                default {
+                                    croak qq|non-sense: rule "$$rule{'name'}", target = ATTRIBUTE, action /= {DROP, ACCEPT QUICK}|;
+                                }
                             }
                         }
+                        when ('ENTRY') {
+                            $main::LOG{'attr_value'}->debug("<$$rule{'index'}> MATCH ENTRY $attr ", $entry->dn);
+                            return 1;
+                        }
                     }
-                    when ('ENTRY') {
-                        $main::LOG{'attr_value'}->debug("<$$rule{'index'}> MATCH ENTRY $attr ", $entry->dn);
-                        return 1;
-                    }
+
+                    last REGEXP;
                 }
-
-                last REGEXP;
             }
         }
     }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.