[Git][debian-gis-team/scripts][master] Check access level before creating/configuring repos.

Bas Couwenberg (@sebastic) gitlab at salsa.debian.org
Tue Aug 9 16:23:11 BST 2022



Bas Couwenberg pushed to branch master at Debian GIS Project / scripts


Commits:
076a4eba by Bas Couwenberg at 2022-08-09T17:23:00+02:00
Check access level before creating/configuring repos.

- - - - -


2 changed files:

- salsa-configure-repositories.pl
- salsa-create-repository.pl


Changes:

=====================================
salsa-configure-repositories.pl
=====================================
@@ -138,6 +138,33 @@ if(!$cfg{namespace}) {
 	exit 1;
 }
 
+# Get user details
+my $user = get_user();
+
+print "user:\n".Dumper($user) if($cfg{debug});
+
+# Get group members
+my $member = get_member($user);
+
+print "member:\n".Dumper($member) if($cfg{debug});
+
+if(ref($member) ne 'HASH') {
+	print "Error: Namespace member not found!\n";
+
+	exit 1;
+}
+elsif(!exists $member->{access_level}) {
+	print "Error: No access level for namespace member!\n";
+
+	exit 1;
+}
+
+if($member->{access_level} < $access_level{Maintainer}) {
+	print "Error: You need Maintainer or Owner access level to configure integrations!\n";
+
+	exit 1;
+}
+
 # Get projects for team
 my $team_projects = get_team_projects();
 
@@ -275,7 +302,58 @@ sub get_namespace {
 		print "Error: Request failed! ($url)\n";
 		print "HTTP Status: ".$res->code." ".$res->message."\n";
 		print $res->content if($res->content);
-	
+
+		exit 1;
+	}
+}
+
+sub get_user {
+	my $url = $cfg{url}.'/user';
+
+	print "Getting user\n" if($cfg{verbose});
+
+	my $req = new HTTP::Request(GET => $url);
+	   $req->header('PRIVATE-TOKEN' => $cfg{token});
+
+	my $res = $ua->request($req);
+	if($res->is_success) {
+		my $content = $res->content;
+
+		return decode_json($content);
+	}
+	else {
+		print "Error: Request failed! ($url)\n";
+		print "HTTP Status: ".$res->code." ".$res->message."\n";
+		print $res->content if($res->content);
+
+		exit 1;
+	}
+}
+
+sub get_member {
+	my ($user) = @_;
+
+	my $url  = $cfg{url}.'/groups/'.uri_escape($cfg{namespace}).'/members';
+	   $url .= '?user_ids='.uri_escape($user->{id});
+
+	print "Getting member for namespace: $cfg{namespace} (id: ". $user->{id} .")\n" if($cfg{verbose});
+
+	my $req = new HTTP::Request(GET => $url);
+	   $req->header('PRIVATE-TOKEN' => $cfg{token});
+
+	my $res = $ua->request($req);
+	if($res->is_success) {
+		my $content = $res->content;
+
+		my $data = decode_json($content);
+
+		return $data->[0];
+	}
+	else {
+		print "Error: Request failed! ($url)\n";
+		print "HTTP Status: ".$res->code." ".$res->message."\n";
+		print $res->content if($res->content);
+
 		exit 1;
 	}
 }
@@ -318,7 +396,7 @@ sub get_team_projects {
 		print "Error: Request failed! ($url)\n";
 		print "HTTP Status: ".$res->code." ".$res->message."\n";
 		print $res->content if($res->content);
-	
+
 		exit 1;
 	}
 }


=====================================
salsa-create-repository.pl
=====================================
@@ -164,6 +164,33 @@ if(!$cfg{namespace}) {
 	exit 1;
 }
 
+# Get user details
+my $user = get_user();
+
+print "user:\n".Dumper($user) if($cfg{debug});
+
+# Get group members
+my $member = get_member($user);
+
+print "member:\n".Dumper($member) if($cfg{debug});
+
+if(ref($member) ne 'HASH') {
+	print "Error: Namespace member not found!\n";
+
+	exit 1;
+}
+elsif(!exists $member->{access_level}) {
+	print "Error: No access level for namespace member!\n";
+
+	exit 1;
+}
+
+if($member->{access_level} < $access_level{Maintainer}) {
+	print "Error: You need Maintainer or Owner access level to configure integrations!\n";
+
+	exit 1;
+}
+
 # Get projects for team
 my $team_projects = get_team_projects();
 
@@ -290,7 +317,58 @@ sub get_namespace {
 		print "Error: Request failed! ($url)\n";
 		print "HTTP Status: ".$res->code." ".$res->message."\n";
 		print $res->content if($res->content);
-	
+
+		exit 1;
+	}
+}
+
+sub get_user {
+	my $url = $cfg{url}.'/user';
+
+	print "Getting user\n" if($cfg{verbose});
+
+	my $req = new HTTP::Request(GET => $url);
+	   $req->header('PRIVATE-TOKEN' => $cfg{token});
+
+	my $res = $ua->request($req);
+	if($res->is_success) {
+		my $content = $res->content;
+
+		return decode_json($content);
+	}
+	else {
+		print "Error: Request failed! ($url)\n";
+		print "HTTP Status: ".$res->code." ".$res->message."\n";
+		print $res->content if($res->content);
+
+		exit 1;
+	}
+}
+
+sub get_member {
+	my ($user) = @_;
+
+	my $url  = $cfg{url}.'/groups/'.uri_escape($cfg{namespace}).'/members';
+	   $url .= '?user_ids='.uri_escape($user->{id});
+
+	print "Getting member for namespace: $cfg{namespace} (id: ". $user->{id} .")\n" if($cfg{verbose});
+
+	my $req = new HTTP::Request(GET => $url);
+	   $req->header('PRIVATE-TOKEN' => $cfg{token});
+
+	my $res = $ua->request($req);
+	if($res->is_success) {
+		my $content = $res->content;
+
+		my $data = decode_json($content);
+
+		return $data->[0];
+	}
+	else {
+		print "Error: Request failed! ($url)\n";
+		print "HTTP Status: ".$res->code." ".$res->message."\n";
+		print $res->content if($res->content);
+
 		exit 1;
 	}
 }
@@ -333,7 +411,7 @@ sub get_team_projects {
 		print "Error: Request failed! ($url)\n";
 		print "HTTP Status: ".$res->code." ".$res->message."\n";
 		print $res->content if($res->content);
-	
+
 		exit 1;
 	}
 }



View it on GitLab: https://salsa.debian.org/debian-gis-team/scripts/-/commit/076a4eba3ea06788a69d3b5fa50d343efafb61f0

-- 
View it on GitLab: https://salsa.debian.org/debian-gis-team/scripts/-/commit/076a4eba3ea06788a69d3b5fa50d343efafb61f0
You're receiving this email because of your account on salsa.debian.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/pkg-grass-devel/attachments/20220809/74b5b2fd/attachment-0001.htm>


More information about the Pkg-grass-devel mailing list