new functions (can_*, is_admin, in_group) for ADCs

(can_* == can_read, can_write, and can_create)

See top of contrib/adc/adc.common-functions for more on this.

Note: the old style (calling get_rights_and_owner with $repo, then
checking $perm_read, $perm_write, etc.), will still work fine.
This commit is contained in:
Sitaram Chamarty 2011-11-22 14:55:15 +05:30
parent d5d982d602
commit af6820a94b
12 changed files with 104 additions and 26 deletions

View file

@ -2,8 +2,7 @@
. $(dirname $0)/adc.common-functions . $(dirname $0)/adc.common-functions
get_rights_and_owner gitolite-admin is_admin || die "just *what* are you trying to pull, young man?"
[ -z "$perm_write" ] && die "just *what* are you trying to pull, young man?"
op=$1 op=$1
shift shift

View file

@ -2,6 +2,24 @@
# please make sure this file is NOT chmod +x # please make sure this file is NOT chmod +x
# this file contains settings for all ADCs at the top, then functions that you
# can call from shell scripts. Other files in this directory have examples.
# all uses require you to "source" this file, like so:
# # at the top of your ADC
# . $(dirname $0)/adc.common-functions
# then you use one of the following functions, like so:
# can_create reponame || die "you can't create reponame"
# can_write reponame || die "you can't write reponame"
# can_read reponame || die "you can't read reponame"
# is_admin || die "you're not an admin"
# IMPORTANT NOTE: all the can_* functions set $repo to the normalised reponame
# (i.e., with '.git' extension removed if it was supplied).
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# settings for various ADCs, collected in one place for ease of keeping local # settings for various ADCs, collected in one place for ease of keeping local
@ -24,6 +42,9 @@ GL_FORKED_FROM="gl-forked-from"
# Change to 1 to make -list the default action for the 'help' command # Change to 1 to make -list the default action for the 'help' command
HELP_LIST_DEFAULT=0 HELP_LIST_DEFAULT=0
# name of "admin" group (see is_admin() below before uncommenting)
# ADMIN_GROUPNAME=admins
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
die() { echo "$@"; exit 1; } die() { echo "$@"; exit 1; }
@ -57,3 +78,55 @@ get_rights_and_owner() {
echo $rights | grep R >/dev/null 2>&1 && perm_read=yes || perm_read= echo $rights | grep R >/dev/null 2>&1 && perm_read=yes || perm_read=
echo $rights | grep W >/dev/null 2>&1 && perm_write=yes || perm_write= echo $rights | grep W >/dev/null 2>&1 && perm_write=yes || perm_write=
} }
can_create() {
get_rights_and_owner ${1%.git}
[ -z "$perm_create" ] && return 1
return 0
}
can_write() {
get_rights_and_owner ${1%.git}
[ -z "$perm_write" ] && return 1
return 0
}
can_read() {
get_rights_and_owner ${1%.git}
[ -z "$perm_read" ] && return 1
return 0
}
# ------------------------------------------------------------------------------
# check if current user is an admin
is_admin() {
# there are two ways to check if someone is an admin. The default (if
# ADMIN_GROUPNAME is not defined) is to check if they have write access to
# the admin repo
if [ -z "$ADMIN_GROUPNAME" ]
then
can_write gitolite-admin || return 1
return 0
fi
# the alternative way is to check membership in $ADMIN_GROUPNAME; please
# remember this method requires GL_BIG_CONFIG to be set
# TODO, pending the code to allow an external query of a user's "group"
# affiliations
in_group $ADMIN_GROUPNAME
}
# ------------------------------------------------------------------------------
grouplist() {
perl -I$GL_BINDIR -Mgitolite -e "cli_grouplist()"
}
in_group() {
local g=$1
grouplist | egrep "(^| )$g( |$)" >/dev/null && return 0
return 1
}

View file

@ -5,12 +5,12 @@
[ -z "$GL_RC" ] && die "ENV GL_RC not set" [ -z "$GL_RC" ] && die "ENV GL_RC not set"
[ -z "$2" ] && die "Usage: fork source_repo target_repo" [ -z "$2" ] && die "Usage: fork source_repo target_repo"
# get_rights_and_owner now also sets $repo; see comments in common functions # all the can_* functions set $repo
get_rights_and_owner $1; from=$repo can_read $1 || die "no read permissions on $repo"
[ -z "$perm_read" ] && die "no read permissions on $from" from=$repo
get_rights_and_owner $2; to=$repo can_create $2 || die "no create permissions on $repo"
[ -z "$perm_create" ] && die "no create permissions on $to" to=$repo
# clone $from to $to # clone $from to $to
git clone --bare -l $GL_REPO_BASE_ABS/$from.git $GL_REPO_BASE_ABS/$to.git git clone --bare -l $GL_REPO_BASE_ABS/$from.git $GL_REPO_BASE_ABS/$to.git

View file

@ -2,8 +2,7 @@
. $(dirname $0)/adc.common-functions . $(dirname $0)/adc.common-functions
get_rights_and_owner gitolite-admin is_admin || die "just *what* are you trying to pull, young man?"
[ -z "$perm_write" ] && die "just *what* are you trying to pull, young man?"
# and here you let them do the dangerous stuff # and here you let them do the dangerous stuff
echo "+rm -rf $GL_REPO_BASE_ABS" echo "+rm -rf $GL_REPO_BASE_ABS"

View file

@ -46,8 +46,7 @@
. $(dirname $0)/adc.common-functions . $(dirname $0)/adc.common-functions
get_rights_and_owner gitolite-admin is_admin || die "just *what* are you trying to pull here, $GL_USER?"
[ -z "$perm_write" ] && die "just *what* are you trying to pull here, $GL_USER?"
pat="$1"; shift pat="$1"; shift
for user for user

View file

@ -16,8 +16,7 @@
. $(dirname $0)/adc.common-functions . $(dirname $0)/adc.common-functions
get_rights_and_owner gitolite-admin is_admin || die "just *what* are you trying to pull here, $GL_USER?"
[ -z "$perm_write" ] && die "just *what* are you trying to pull here, $GL_USER?"
# find the command name; we don't do a lot of fancy checking -- we just go # find the command name; we don't do a lot of fancy checking -- we just go
# "safe" and assume that anything but a name of "su-setperms" runs getperms # "safe" and assume that anything but a name of "su-setperms" runs getperms

View file

@ -13,8 +13,7 @@
. $(dirname $0)/adc.common-functions . $(dirname $0)/adc.common-functions
get_rights_and_owner gitolite-admin is_admin || die "just *what* are you trying to pull, young man?"
[ -z "$perm_write" ] && die "just *what* are you trying to pull, young man?"
user="$1"; shift user="$1"; shift
cmd="$1"; shift cmd="$1"; shift

View file

@ -23,8 +23,10 @@
# of git-symbolic-ref to also work # of git-symbolic-ref to also work
[ -z "$2" ] && die "usage: symbolic-ref /path/to/repo.git <arguments to git-symbolic-ref>" [ -z "$2" ] && die "usage: symbolic-ref /path/to/repo.git <arguments to git-symbolic-ref>"
get_rights_and_owner $1; to=$repo # all the can_* functions set $repo
[ -z "$perm_write" ] && die "no write permissions on $to" can_write $1 || die "no write permissions on $repo"
to=$repo
shift shift
# change head # change head

View file

@ -52,9 +52,8 @@
#[ -z "$perm_write" ] && die "just *what* are you trying to pull, young man?" #[ -z "$perm_write" ] && die "just *what* are you trying to pull, young man?"
#get_rights_and_owner $1; #get_rights_and_owner $1;
# Comment this block if uncommenting the above block # all the can_* functions set $repo
get_rights_and_owner $1; can_read $1 || die "no read permissions on $repo"
[ -z "$perm_read" ] && die "no read permissions on $repo"
cmd=$2 cmd=$2
identifier=$3 identifier=$3

View file

@ -10,9 +10,8 @@
sha=$2 sha=$2
[ -n "$sha" ] || die Usage: ssh ... who-pushed reponame SHA \# at least first few hex digits [ -n "$sha" ] || die Usage: ssh ... who-pushed reponame SHA \# at least first few hex digits
# get_rights_and_owner now also sets $repo; see comments in common functions # all the can_* functions set $repo
get_rights_and_owner $1 can_read $1 || die "no read permissions on $repo"
[ -z "$perm_read" ] && die "no read permissions on $repo"
cd $GL_REPO_BASE_ABS/$repo.git cd $GL_REPO_BASE_ABS/$repo.git

View file

@ -128,9 +128,9 @@ like `_____R__W u1` or maybe `____@R_@W <gitolite>`. (The `u1` indicates the
queried repo is a wildcard repo created by user `u1`; for meanings of the "@" queried repo is a wildcard repo created by user `u1`; for meanings of the "@"
see doc/report-output.mkd) see doc/report-output.mkd)
But that's cumbersome. There's a bash shell function called But that's cumbersome. It's much nicer to use the convenient functions
`get_rights_and_owner` in `contrib/adc/adc.common-functions` that is much more defined in `contrib/adc/adc.common-functions`; see the comments in that file
convenient. See any of the other samples for how to use it. for details, and any of the other samples for how to use them.
If you prefer perl, there is a nicely commented example in If you prefer perl, there is a nicely commented example in
`contrib/adc/get-rights-and-owner.in-perl`. `contrib/adc/get-rights-and-owner.in-perl`.

View file

@ -8,6 +8,7 @@ use Exporter 'import';
check_ref check_ref
check_repo_write_enabled check_repo_write_enabled
cli_repo_rights cli_repo_rights
cli_grouplist
dbg dbg
dos2unix dos2unix
list_phy_repos list_phy_repos
@ -837,6 +838,15 @@ sub cli_repo_rights {
print join(" ", check_access($_[0])), "\n"; print join(" ", check_access($_[0])), "\n";
} }
# helper/convenience routine to get group membership info
sub cli_grouplist {
die "GL_BIG_CONFIG needs to be set\n" unless $GL_BIG_CONFIG;
# we may not have any data yet...
parse_acl() unless (%repos);
my @groups = grep { s/^@//; } get_memberships($ENV{GL_USER}, 0);
print join(" ", @groups), "\n";
}
sub can_read { sub can_read {
my $repo = shift; my $repo = shift;
my $user = shift || $ENV{GL_USER}; my $user = shift || $ENV{GL_USER};