Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add an option to remove the webapp user

nimbus-remove-user did not allow to remove a user from the webapp.
Since the user was still in the webapp DB, it could not be added again.
nimbus-remove-user now has -W and -w options to remove entries from the
webapp too.

Closes #78.
  • Loading branch information...
commit a1033f6b4efa02e7810146b293a4821a06e6b7ca 1 parent 8848c9c
@priteau priteau authored
View
3  home/bin/nimbus-remove-user
@@ -13,6 +13,9 @@ NIMBUS_PYSRC="$NIMBUS_WEBDIR/src/python"
PYTHONPATH="$NIMBUS_PYSRC:$NIMBUS_PYLIB:$PYTHONPATH"
export PYTHONPATH
+DJANGO_SETTINGS_MODULE="nimbusweb.portal.settings"
+export DJANGO_SETTINGS_MODULE
+
source $NIMBUS_HOME/cumulus/env.sh
export CUMULUS_SETTINGS_FILE=$NIMBUS_HOME/cumulus/etc/cumulus.ini
source $NIMBUS_HOME/ve/bin/activate
View
31 home/libexec/nimbus_remove_user.py
@@ -33,7 +33,7 @@
def get_nimbus_home():
"""Determines home directory of Nimbus install we are using.
-
+
First looks for a NIMBUS_HOME enviroment variable, else assumes that
the home directory is the parent directory of the directory with this
script.
@@ -53,6 +53,10 @@ def setup_options(argv):
Remove a nimbus user
"""
(parser, all_opts) = pynimbusauthz.get_default_options(u)
+ opt = cbOpts("web_id", "w", "Set the web user name to remove. If not set and the user is to be removed from webapp, a username will be created from the email address.", None)
+ all_opts.append(opt)
+ opt = cbOpts("web", "W", "Remove user from webapp", False, flag=True)
+ all_opts.append(opt)
(o, args) = pynimbusauthz.parse_args(parser, all_opts, argv)
# def verify_opts(o, args, parser):
@@ -66,7 +70,7 @@ def remove_gridmap(dn):
configpath = os.path.join(nimbus_home, 'nimbus-setup.conf')
config = SafeConfigParser()
if not config.read(configpath):
- raise CLIError('ENIMBUSHOME',
+ raise CLIError('ENIMBUSHOME',
"Failed to read config from '%s'. Has Nimbus been configured?"
% configpath)
gmf = config.get('nimbussetup', 'gridmap')
@@ -116,9 +120,32 @@ def delete_user(o):
except Exception, ex:
print "WARNING %s" % (ex)
+ if o.web:
+ if o.web_id == None:
+ o.web_id = o.emailaddr.split("@")[0]
+ remove_web(o)
+
user.destroy_brutally()
db.commit()
+def remove_web(o):
+ # import this here because otherwise errors will be thrown when
+ # the settings.py is imported (transitively). Web is disabled by
+ # default in a Nimbus install, we should keep the experience cleanest
+ # for new admins.
+ try:
+ import nimbusweb.portal.nimbus.remove_web_user as remove_web_user
+ except Exception, e:
+ msg = "\nERROR linking with web application (have you ever sets up the web application?)\n"
+ msg += "\nSee: http://www.nimbusproject.org/docs/current/admin/reference.html#nimbusweb-config\n"
+ msg += "\n%s\n" % e
+ raise CLIError('EUSER', "%s" % msg)
+
+ errmsg = remove_web_user.remove_web_user(o.web_id)
+
+ if errmsg:
+ raise CLIError('EUSER', "Problem removing user from webapp: %s" % (errmsg))
+
def main(argv=sys.argv[1:]):
try:
View
13 web/src/python/nimbusweb/portal/nimbus/models.py
@@ -1,6 +1,8 @@
import sys
+
from django.db import models
from django.contrib.auth.models import User, UserManager
+from django.core.exceptions import ObjectDoesNotExist
class TokenFailure(models.Model):
ip = models.IPAddressField(primary_key=True)
@@ -10,7 +12,7 @@ class TokenFailure(models.Model):
class UserProfile(models.Model):
"""Extension to properties in django's user model, we hang things off the user record."""
-
+
# Required field
user = models.ForeignKey(User, unique=True, related_name="%(class)s_related")
@@ -29,13 +31,16 @@ class UserProfile(models.Model):
query_secret = models.TextField(null=True)
cloudprop_file = models.TextField(null=True)
nimbus_userid = models.TextField(null=True)
-
def user_post_save(sender, instance, **kwargs):
profile, new = UserProfile.objects.get_or_create(user=instance)
models.signals.post_save.connect(user_post_save, User)
def user_post_delete(sender, instance, **kwargs):
- profile = UserProfile.objects.get_or_create(user=instance)
- profile.delete()
+ try:
+ profile = UserProfile.objects.get(user=instance)
+ profile.delete()
+ except ObjectDoesNotExist:
+ pass
+
models.signals.post_delete.connect(user_post_delete, User)
View
34 web/src/python/nimbusweb/portal/nimbus/remove_web_user.py
@@ -0,0 +1,34 @@
+"""This is used as a programmatic way to remove user credentials and information
+from the webapp system"""
+
+import adminops
+import nimbusweb.portal.settings as settings
+import os
+import sys
+from django.contrib.auth.models import User
+from django.db import IntegrityError
+from datetime import datetime
+from dateutil.relativedelta import *
+
+from django.contrib.auth.models import User
+
+import nimbusweb.portal.settings as settings
+
+def remove_web_user(username):
+ """Remove user and return error_msg or None if success"""
+ try:
+ user = User.objects.get(username=username)
+ user.delete()
+ except:
+ exception_type = sys.exc_type
+ try:
+ exceptname = exception_type.__name__
+ except AttributeError:
+ exceptname = exception_type
+ name = str(exceptname)
+ err = str(sys.exc_value)
+ errmsg = "%s: %s" % (name, err)
+ return errmsg
+
+ # success
+ return None
Please sign in to comment.
Something went wrong with that request. Please try again.