File: permission-viewer

package info (click to toggle)
movabletype-opensource 5.1.4%2Bdfsg-4%2Bdeb7u3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 32,996 kB
  • sloc: perl: 197,285; php: 62,405; sh: 166; xml: 117; makefile: 83; sql: 32
file content (144 lines) | stat: -rwxr-xr-x 4,202 bytes parent folder | download
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
#!/usr/bin/perl
package MT::Permission::Viewer;
use strict;
use warnings;
use lib qw( lib ../lib extlib ../extlib );
use base qw( MT::Tool );

my $VERSION = 0.1;

## Currently MT::Tool doesn't support this yet.
## this is a fake.
sub version { $VERSION }

sub help {
    return <<'HELP';
OPTIONS:
    -l, --list=OPTION      prints list of installed elements. OPTION receives <actions> or <permissions>.
    -a, --action=NAME      prints detail of action.
    -p, --permission=NAME  prints detail of permission.
    -v, --verbose          more details for list, action, permission...
    -h, --help             this.
HELP
}

sub usage {
    return '[-list|-action|-permission]';
}


## options
my ( $action, $permission, $list );

sub options {
    return (
        'action=s'     => \$action,
        'permission=s' => \$permission,
        'list=s'       => \$list,
)}

sub main {
    my $class = shift;
    my ($verbose) = $class->SUPER::main(@_);
    my $out = $list       ? $class->list($list, $verbose)
            : $action     ? $class->action($action, $verbose)
            : $permission ? $class->permission($permission, $verbose)
            :               $class->show_help()
            ; 
    print "$out\n";
}

sub list {
    my $class = shift;
    my ($list, $verbose)  = @_;
    my $out;
    my $show_acts = $list =~ /actions?|acts?/;
    my $show_perms = $list =~ /permissions?|perms?/;
    my $both = !$show_acts && !$show_perms;
    my $ps = MT->registry('permissions');
    my @ps = sort { $a cmp $b } keys %$ps ;
    my (%exist_action, @exist_actions);
    if ( $both || $show_acts ) {
        for my $perm_key ( keys %$ps ) {
            my $p = $ps->{$perm_key};
            my $act_for_this = $p->{permitted_action};
            for my $k ( keys %$act_for_this ) {
                $exist_action{$k} = $perm_key;
            }
        }
        @exist_actions = sort { $a cmp $b } keys %exist_action;
    }
    if ( $show_perms || $both ) {
        $out .= "**** List of all permissions ****\n" if $both;
        $out .= "$_\n" for @ps;
    }
    if ( $show_acts || $both ) {
        $out .= "**** List of all actions ****\n" if $both;
        $out .= ( $verbose ? "$_\t defined at $exist_action{$_}\n" : "$_\n" ) for @exist_actions;
    }
    $out;
}

sub action {
    my $class = shift;
    my ($action, $verbose)  = @_;
    my $out;
    $out .= "*** information for $action ***\n" if $verbose;
    my $ps = MT->registry('permissions');
    if ( $verbose ) {
        my @ps = sort keys %$ps;
        my ( %permit, %reject );
        for my $p ( @ps ) {
            MT::Permission->_confirm_action( $p, $action ) ? $permit{$p} = 1
                                                           : $reject{$p} = 1;
        }
        for my $p ( @ps ) {
            $out .= ( $permit{$p} ? "+++ " : "    ");
            $out .= "$p\n";
        }
    }
    else {
        my @perms = sort grep { MT::Permission->_confirm_action( $_, $action ) } keys %$ps;
        $out .= "$_\n" for @perms;
    }
    $out || 'No such action';
}

sub permission {
    my $class = shift;
    my ($permission, $verbose)  = @_;
    my $out;
    my $ps = MT->registry('permissions');
    my $target_perm = $ps->{$permission} or die "Unknown permission: $permission\n";
    my $printer;
    my %done;
    $printer = sub {
        my ($perm, $indent) = @_;
        $indent ||= '';
        my $actions = $perm->{permitted_action};
        my @actions = sort keys %$actions;
        my $inherits = $perm->{inherit_from};
        my @inherits = defined $inherits ? sort @$inherits : ();
        for my $action ( @actions ) {
            my $val = $actions->{$action};
            if ( $verbose ) {
                $out .= "$indent$action\t: $val\n";
            }
            else {
                $out .= "$action\t: $val\n" if !$done{$action};
                $done{$action} = 1;
            }
        }
        for my $parent ( @inherits ) {
            $out .= "$indent---- ( inherit from: $parent )\n" if $verbose;
            $parent = $ps->{$parent};
            $printer->( $parent, $indent . '    ' );
        }
    };
    $printer->( $target_perm );
    $out;
}

__PACKAGE__->main() unless caller;