Add actions for LDAP group management.

This commit is contained in:
James Valleroy 2015-07-11 14:59:15 -04:00 committed by Sunil Mohan Adapa
parent 2adf14b8b7
commit 197c95a7bf
5 changed files with 104 additions and 21 deletions

43
actions/add-ldap-user-to-group Executable file
View File

@ -0,0 +1,43 @@
#!/bin/bash
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Must be run as root.
username="$1"
groupname="$2"
# check if group already exists
results=$(ldapsearch -Y EXTERNAL -H ldapi:/// -b 'ou=groups,dc=thisbox' -LLL "(cn=$groupname)" cn)
if [ -z "$results" ]; then
# create group, with user as initial member
cat <<EOF |ldapadd -Y EXTERNAL -H ldapi:///
dn: cn=$groupname,ou=groups,dc=thisbox
objectClass: groupOfUniqueNames
cn: $groupname
uniqueMember: uid=$username,ou=users,dc=thisbox
EOF
else
# add user to existing group
cat <<EOF |ldapmodify -Y EXTERNAL -H ldapi:///
dn: cn=$groupname,ou=groups,dc=thisbox
changetype: modify
add: uniqueMember
uniqueMember: uid=$username,ou=users,dc=thisbox
EOF
fi

View File

@ -58,24 +58,3 @@ if [ $? -ne 0 ]; then
echo "Failed to create posix account for user"
exit 2
fi
# check if admin group exists
results=$(ldapsearch -Y EXTERNAL -H ldapi:/// -b 'ou=groups,dc=thisbox' -LLL "(cn=admin)" cn)
if [ -z "$results" ]; then
# create admin group, with new user as a member
cat <<EOF |ldapadd -Y EXTERNAL -H ldapi:///
dn: cn=admin,ou=groups,dc=thisbox
objectClass: groupOfUniqueNames
cn: admin
uniqueMember: uid=$username,ou=users,dc=thisbox
EOF
else
# add new user to existing admin group
cat <<EOF |ldapmodify -Y EXTERNAL -H ldapi:///
dn: cn=admin,ou=groups,dc=thisbox
changetype: modify
add: uniqueMember
uniqueMember: uid=$username,ou=users,dc=thisbox
EOF
fi

View File

@ -0,0 +1,46 @@
#!/bin/sh
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Must be run as root.
username="$1"
groupname="$2"
cat <<EOF |ldapmodify -Y EXTERNAL -H ldapi:///
dn: cn=$groupname,ou=groups,dc=thisbox
changetype: modify
delete: uniqueMember
uniqueMember: uid=$username,ou=users,dc=thisbox
EOF
if [ $? -eq 0 ]; then
echo "Removed user from group"
elif [ $? -eq 16 ]; then
echo "User was not in group"
exit 1
elif [ $? -eq 65 ]; then
# Cannot have empty group, so just delete the group.
ldapdelete -Y EXTERNAL -H ldapi:/// "cn=$groupname,ou=groups,dc=thisbox"
if [ $? -eq 0 ]; then
echo "User was last member in group, so group was deleted."
else
echo "User was last member in group, but could not delete group."
exit 1
fi
fi

View File

@ -72,6 +72,14 @@ than 63 characters in length.'),
messages.error(self.request,
_('Creating LDAP user failed.'))
try:
actions.superuser_run(
'add-ldap-user-to-group',
[user.get_username(), 'admin'])
except ActionError:
messages.error(self.request,
_('Failed to add new user to admin group.'))
self.login_user()
return user

View File

@ -56,6 +56,13 @@ class CreateUserForm(UserCreationForm):
messages.error(self.request,
_('Creating LDAP user failed.'))
try:
actions.superuser_run(
'add-ldap-user-to-group',
[user.get_username(), 'admin'])
except ActionError:
messages.error(self.request,
_('Failed to add new user to admin group.'))
return user