Author: justi9
Date: 2008-09-26 15:25:40 -0400 (Fri, 26 Sep 2008)
New Revision: 2548
Modified:
mgmt/trunk/cumin/python/cumin/tools.py
Log:
Refactor add user, and add a change password admin command
Modified: mgmt/trunk/cumin/python/cumin/tools.py
===================================================================
--- mgmt/trunk/cumin/python/cumin/tools.py 2008-09-26 19:24:30 UTC (rev 2547)
+++ mgmt/trunk/cumin/python/cumin/tools.py 2008-09-26 19:25:40 UTC (rev 2548)
@@ -2,14 +2,26 @@
from parsley.command import *
from mint import *
-from crypt import crypt
from getpass import getpass
-from random import sample
from psycopg2 import IntegrityError
from cumin import CuminConfig
from util import *
+def prompt_password():
+ password = None
+
+ while password is None:
+ once = getpass("Type password: ")
+ twice = getpass("Retype password: ")
+
+ if once == twice:
+ password = once
+ else:
+ print "Passwords don't match; try again"
+
+ return password
+
class BaseCuminTool(Command):
def __init__(self, name):
super(BaseCuminTool, self).__init__(None, name)
@@ -140,6 +152,10 @@
command.description = "Remove USER from ROLE"
command.arguments = ("USER", "ROLE")
+ command = self.ChangePassword(self, "change-password")
+ command.description = "Change password of USER"
+ command.arguments = ("USER",)
+
def init(self):
super(CuminAdminTool, self).init()
@@ -178,20 +194,8 @@
print "Error: a user called '%s' already exists" %
name
sys.exit(1)
- password = None
+ crypted = crypt_password(prompt_password())
- while password is None:
- once = getpass("Set password: ")
- twice = getpass("Retype password: ")
-
- if once == twice:
- password = once
- else:
- print "Passwords don't match; try again"
-
- chs =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
- crypted = crypt(password, "".join(sample(chs, 2)))
-
try:
subject = Subject(name=name, password=crypted)
@@ -311,4 +315,20 @@
class ChangePassword(Command):
def run(self, opts, args):
- pass
+ try:
+ ssubject = args[1]
+ except IndexError:
+ raise CommandException(self, "USER is required")
+
+ subject = Subject.getByName(ssubject)
+
+ if not subject:
+ raise CommandException\
+ (self, "User '%s' is unknown" % subject.name)
+
+ crypted = crypt_password(prompt_password())
+
+ subject.password = crypted
+ subject.syncUpdate()
+
+ print "Password of user '%s' is changed" % subject.name
Show replies by date