File: db_user_admin.rules

package info (click to toggle)
polymake 4.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 31,528 kB
  • sloc: cpp: 152,204; perl: 43,222; javascript: 30,700; ansic: 2,937; java: 2,654; python: 641; sh: 244; xml: 117; makefile: 62
file content (308 lines) | stat: -rw-r--r-- 8,590 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#  Copyright (c) 1997-2020
#  Ewgenij Gawrilow, Michael Joswig, and the polymake team
#  Technische Universität Berlin, Germany
#  https://polymake.org
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#-------------------------------------------------------------------------------
#
#  This file is part of the polymake database interface polyDB.
#
#   @author Silke Horn, Andreas Paffenholz
#   http://www.mathematik.tu-darmstadt.de/~paffenholz
#

package PolyDB::Client;

# @category Database Access
user_method change_password($) {
   my ($self, $pwd) = @_;

   my $db = try {
      $self->SUPER::get_database($default::db_auth_db);
   } catch {
      die_neatly($_);
   };
   my $command = [
         updateUser => $self->username,
         pwd => $pwd,
         mechanisms => [ "SCRAM-SHA-1", "SCRAM-SHA-256" ]
   ];

   try {
      $db->run_command($command);
   } catch {
      die_neatly($_);
   };

}

# @category Database Administration
# options for creating a user
options %create_user_options = (
   # String user the username
   user => undef, 
   # String password the passowrd
   password => undef,
   #Bool public_read_access whether the new user should have read access to all public collections, default true
   public_read_access => true, 
   # ARRAY collections a list of collections the user should have read access to
   collections => [],
   # ARRAY admin_collections a list of collections the user should have read and write access to
   admin_collections => []
);


# @category Database Administration
# create a new user
# @options %create_user_options
user_method create_user (%create_user_options) {
   my ($self, $options) = @_;

   if ( !$options->{user} || !$options->{password} ) {
      croak("user and passowrd must be given\n");
   }
   croak "user already exists\n" if $self->user_exists($options->{user});

   my $roles = [ "changeOwnAccount" ];
   if ( $options->{public_read_access}) {
      push @$roles, { role => 'polymakeUser', db => $PolyDB::default::db_auth_db };
   }
   
   foreach my $col (@{$options->{collections}}) {
      if ($col !~ /\./) {
         $col = disambiguate_collection_name($self, $col);
      }
      push @$roles, { role => $col, db => $PolyDB::default::db_auth_db };
   }

   foreach my $col (@{$options->{admin_collections}}) {
      if ($col !~ /\./) {
         $col = disambiguate_collection_name($self, $col);
      }
      push @$roles, { role => $col.".admin", db => $PolyDB::default::db_auth_db };
   }

   my $command = [
      createUser   => $options->{user},
      pwd => $options->{password},
      roles => $roles
   ];

   my $db = try {
      $self->SUPER::get_database($default::db_auth_db);
   } catch {
      die_neatly($_);
   };
   
   try {
      $db->run_command($command);
   } catch {
      die_neatly($_);
   };

   return $options->{user};
}

# @category Database Administration
# add a user to a collection
# @option String user the username
# @option String collection the collection
# @option Bool admin whether the user should get write access, default false
user_method add_user_to_collection( { user=>undef, collection=>$default::db_collection_name, admin=>false } ) {
   my ($self,$options) = @_;

   croak "username must be given\n" if !$options->{user};
   my $collection = $options->{collection};
   croak "no collection name specified and PolyDB::default::db_collection_name not set\n" if !$collection;
   if ($collection !~ /\./) {
      $collection = disambiguate_collection_name($self, $collection);
   }

   my $db = try {
      $self->SUPER::get_database($default::db_auth_db);
   } catch {
      die_neatly($_);
   };

   my $roles = [ $collection ];
   push @$roles, $collection.".admin" if $options->{admin};
   my $command = [
      grantRolesToUser => $options->{user},
      roles => $roles
   ];
   
   try {
      $db->run_command($command);
   } catch {
      die_neatly($_);
   };

   return [$options->{user}, $collection];
}

# @category Database Administration
# remove a user to a collection
# note: access to public collections is not removed by this
# @option String user the username
# @option String collection the collection
user_method remove_user_from_collection({user => undef, collection => $default::db_collection_name}) {
   my ($self,$options) = @_;

   die "user and collection must be given\n" if !(defined($options->{user}) && defined($options->{collection}));
   my $collection = $options->{collection};
   if ($collection !~ /\./) {
      $collection = disambiguate_collection_name($self, $collection);
   }

   my $db = try {
      $self->SUPER::get_database($default::db_auth_db);
   } catch {
      die_neatly($_);
   };

   my $roles = [ $collection, $collection.".admin" ];
   my $command = [
      revokeRolesFromUser   => $options->{user},
      roles => $roles
   ];

   try {
      $db->run_command($command);
   } catch {
      die_neatly($_);
   };
}

# @category Database Administration
# delete a user
# @param String user the username
user_method delete_user($) {
   my ($self,$user) = @_;

   return if !$self->user_exists($user);

   my $db = try {
      $self->SUPER::get_database($default::db_auth_db);
   } catch {
      die_neatly($_);
   };

   my $command = [
      dropUser   => $user,
   ];

   $db->run_command($command);
   return $user;
}

# @category Database Administration
# create a database administrator
# @option String user the username
# @option String password the password
user_method create_database_admin($, $ ) {
   my ($self, $user, $password) = @_;

   croak("User already exists\n") if $self->user_exists($user);
   my $db = try {
      $self->SUPER::get_database($default::db_auth_db);
   } catch {
      die_neatly($_);
   };

   my $command = [
      createUser   => $user,
      pwd => $password,
      roles => [ 
         { db => $PolyDB::default::db_auth_db, role => "userAdmin" }, 
         { db => $PolyDB::default::db_name, role => "readWrite" } 
      ]
   ];

   $db->run_command($command);
   return $user;
}


# @category Database Administration
# get all users
# @param ARRAY usernames list of usernames
# return HASH
user_method get_users({ usernames=>undef, with_permissions=>false } ) {
   my ($self, $options ) = @_;

   my $db = try {
      $self->SUPER::get_database($default::db_auth_db);
   } catch {
      die_neatly($_);
   };

   my $command;
   my $user_query;
   if ( defined( $options->{usernames} ) ) {
      my $users = [];
      foreach my $user (@$usernames) {
         push @$user_query, ( user=>$user, db=>$PolyDB::default::db_auth_db);
      }
      $command = [
         usersInfo => $user_query
      ];   
   } else {
      $command = [
         usersInfo => 1
      ];
   }

   my $res = $db->run_command($command);

   my $users;
   foreach my $u (@{$res->{users}}) {
      my $user = { canChangePassword => false, hasPublicAccess => false, collections=>{}, admin_collections => {}, admin_roles => {} };
      foreach my $role (@{$u->{roles}} ) {
         if ( $role->{role} eq "polymakeUser" ) {
            $user->{hasPublicAccess} = true;
            next;
         }
         if ( $role->{role} eq "changeOwnAccount" ) {
            $user->{canChangePassword} = true;
            next;
         }
         if ( $role->{role} eq "changeOwnAccount" ) {
            $user->{canChangePassword} = true;
            next;
         }
         if ( $role->{role} =~ /[\w]+\.[\w.]+\.admin$/ ) {
            (my $collection = $role->{role}) =~ s/.admin$//;
            $user->{admin_collections}->{$collection} = true;
            next;
         }
         if ( $role->{role} =~ /[\w]+\.[\w.]+$/ ) {
            $user->{collections}->{$role->{role}} = true;
            next;
         }
         foreach my $r (keys %{$user->{collections}}) {
            if ( exists($user->{admin_collections}->{$r}) ) {
               delete($usrt->{collections}->{$r});
            }
         }
         $user->{admin_roles}->{$role->{role}} = true;
      }
      $users->{$u->{user}} = $user;
   }

   return $users;
}

# Local Variables:
# mode: perl
# cperl-indent-level:3
# indent-tabs-mode:nil
# End: