new mirror function: 'status all all'
[gitolite.git] / src / commands / perms
blob30984bfc55e6cbb0ba4a92792a30c89b358c5418
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
5 use lib $ENV{GL_LIBDIR};
6 use Gitolite::Rc;
7 use Gitolite::Common;
8 use Gitolite::Easy;
10 =for usage
11 perms -- list or set permissions for user-created ("wild") repo.
13 Usage summary:
14 ssh git@host perms <repo> -l
15 # list current permissions on repo
16 ssh git@host perms <repo> -lr
17 # list available roles and their access rights
19 ssh git@host perms <repo> + <rolename> <username>
20 # change permissions: add a user to a role
21 ssh git@host perms <repo> - <rolename> <username>
22 # change permissions: remove a user from a role
24 Examples:
25 ssh git@host perms my/repo + READERS alice
26 ssh git@host perms my/repo + WRITERS bob
28 ----
29 There is also a batch mode useful for scripting and bulk loading. Do not
30 combine this with the +/- mode above. This mode also accepts an optional "-c"
31 flag to create the repo if it does not already exist (assuming $GL_USER has
32 permissions to create it).
34 Examples:
35 cat copy-of-backed-up-gl-perms | ssh git@host perms <repo>
36 cat copy-of-backed-up-gl-perms | ssh git@host perms -c <repo>
37 =cut
39 usage() if not @ARGV or $ARGV[0] eq '-h';
41 $ENV{GL_USER} or _die "GL_USER not set";
43 my $generic_error = "repo does not exist, or you are not authorised";
45 if ( @ARGV >= 2 and $ARGV[1] eq '-l' ) {
46 getperms($ARGV[0]); # doesn't return
49 # auto-create the repo if -c passed and repo doesn't exist
50 if ( $ARGV[0] eq '-c' ) {
51 shift;
52 my $repo = $ARGV[0] or usage();
53 _die "invalid repo '$repo'" unless $repo =~ $REPONAME_PATT;
55 if ( not -d "$rc{GL_REPO_BASE}/$repo.git" ) {
56 my $ret = Gitolite::Conf::Load::access( $repo, $ENV{GL_USER}, '^C', 'any' );
57 _die $generic_error if $ret =~ /DENIED/;
59 require Gitolite::Conf::Store;
60 Gitolite::Conf::Store->import;
61 new_wild_repo( $repo, $ENV{GL_USER}, 'perms-c' );
62 gl_log( 'create', $repo, $ENV{GL_USER}, 'perms-c' );
66 my $repo = shift;
68 if ( @ARGV and $ARGV[0] eq '-lr' ) {
69 list_roles();
70 exit 0;
71 } else {
72 setperms(@ARGV);
75 # cache control
76 if ($rc{CACHE}) {
77 require Gitolite::Cache;
78 Gitolite::Cache::cache_control('flush', $repo);
81 _system( "gitolite", "trigger", "POST_CREATE", $repo, $ENV{GL_USER}, 'perms' );
83 # ----------------------------------------------------------------------
85 sub getperms {
86 my $repo = shift;
87 _die $generic_error if not owns($repo);
88 my $pf = "$rc{GL_REPO_BASE}/$repo.git/gl-perms";
90 print slurp($pf) if -f $pf;
92 exit 0;
95 sub setperms {
96 _die $generic_error if not owns($repo);
97 my $pf = "$rc{GL_REPO_BASE}/$repo.git/gl-perms";
99 if ( not @_ ) {
100 # legacy mode; pipe data in
101 print STDERR "'batch' mode started, waiting for input (run with '-h' for details).\n";
102 print STDERR "Please enter 'cancel' to abort if you did not intend to do this.\n";
103 @ARGV = ();
104 my @a;
105 while (<>) {
106 _die "CANCELLED" if /^\s*cancel\s*$/i;
107 invalid_role($1) if /(\S+)/ and not $rc{ROLES}{$1};
108 push @a, $_;
111 _print( $pf, @a );
112 return;
115 _die "Invalid syntax. Please re-run with '-h' for detailed usage" if @_ != 3;
116 my ( $op, $role, $user ) = @_;
117 _die "Invalid syntax. Please re-run with '-h' for detailed usage" if $op ne '+' and $op ne '-';
118 _die "Invalid user '$user'" if not $user =~ $USERNAME_PATT;
120 my $text = '';
121 my @text = slurp($pf) if -f $pf;
123 my $present = grep { $_ eq "$role $user\n" } @text;
125 if ( $op eq '-' ) {
126 if ( not $present ) {
127 _warn "'$role $user' was not present in file";
128 } else {
129 @text = grep { $_ ne "$role $user\n" } @text;
130 _print( $pf, @text );
132 } else {
133 invalid_role($role) unless grep { $_->[3] eq $role } load_roles();
134 if ($present) {
135 _warn "'$role $user' already present in file";
136 } else {
137 push @text, "$role $user\n";
138 @text = sort @text;
139 _print( $pf, @text );
144 my @rules;
146 sub load_roles {
147 return @rules if @rules;
149 require Gitolite::Conf::Load;
150 Gitolite::Conf::Load::load($repo);
152 my %repos = %Gitolite::Conf::Load::repos;
153 my @repo_memberships = Gitolite::Conf::Load::memberships('repo', $repo);
155 for my $rp (@repo_memberships) {
156 my $hr = $repos{$rp};
157 for my $r ( keys %$hr ) {
158 next unless $r =~ s/^@//;
159 next unless $rc{ROLES}{$r};
160 map { $_->[3] = $r } @{ $hr->{"\@$r"} };
161 push @rules, @{ $hr->{"\@$r"} };
164 return @rules;
167 sub invalid_role {
168 my $role = shift;
170 print STDERR "Invalid role '$role'; valid roles for this repo:\n";
171 open(STDOUT, '>&', \*STDERR); # make list_roles print to STDERR
172 list_roles();
173 exit 1;
176 sub list_roles {
178 my @rules = sort { $a->[0] <=> $b->[0] } load_roles();
180 for (@rules) {
181 $_->[2] =~ s(^refs/heads/)();
182 $_->[2] = '--any--' if $_->[2] eq 'refs/.*';
185 my $max = 0;
186 map { $max = $_ if $_ > $max } map { length($_->[2]) } @rules;
187 printf("\t%s\t%*s\t \t%s\n", "perm", -$max, "ref", "role");
188 printf("\t%s\t%*s\t \t%s\n", "----", -$max, "---", "----");
189 printf("\t%s\t%*s\t=\t%s\n", $_->[1], -$max, $_->[2], $_->[3]) for @rules;