#!/usr/bin/python2.4 # (c) Fry-IT Ltd # Peter Bengtsson, peter@fry-it.com # License: ZPL """ Change the password of existing user inside zope. """ usage=""" python %s -u username:password [-p /path/to/zope/lib/python] [-P portnumber] [-d domain] """ % __file__ __version__='0.0.1' from pprint import pprint import sys, getopt, os, string, time import types COLOUR_OUT = True ServerError='' verbose=0 old=0 doctor=0 index_html=0 def wrap_out(s, colour=None): colours={"default":"", "blue": chr(27) + "[01;34m", "cyan": chr(27) + "[01;36m", "green": chr(27) + "[01;32m", "red": chr(27) + "[01;31m" } if COLOUR_OUT and sys.platform != 'win32' and colours.has_key(colour): return colours[colour] + str(s) + chr(27) + "[00m" return s def log(s): pass def main(): user, password = None, None opts, args = getopt.getopt(sys.argv[1:], 'p:u:r:Pd') havepath = None global port port = '8080' global domain domain = 'localhost' for o, v in opts: if o=='-p': d, f = os.path.split(v) if f=='ZPublisher': sys.path.insert(0,d) else: sys.path.insert(0,v) havepath=1 elif o=='-u': v = string.split(v,':') user, password = v[0], string.join(v[1:],':') elif o=='-v': verbose=1 elif o=='-P': port=v elif o=='-d': domain=v if not user or not password: print sys.argv[0]+usage sys.exit(1) if not havepath: here=os.path.split(sys.argv[0])[0] if os.path.exists(os.path.join(here,'ZPublisher')): sys.path.insert(0,here) else: here=os.path.split(here)[0] here=os.path.join(here,'lib','python') if os.path.exists(os.path.join(here,'ZPublisher')): sys.path.insert(0,here) url = 'http://%s:%s/acl_users' % (domain, port) import getpass newpassword = getpass.getpass(prompt='New password:') again_newpassword = getpass.getpass(prompt='Again (leave blank if you\'re confident):') if again_newpassword and again_newpassword != newpassword: print >>sys.stderr, "Password mismatch" sys.exit(1) start(url, user, password, newpassword) def start(url, user, password, newpassword): import ZPublisher.Client global ServerError ServerError=ZPublisher.Client.ServerError acl_users=ZPublisher.Client.Object(url, username=user, password=password) from Globals import DevelopmentMode kw = {'name':user, 'password':newpassword, 'confirm':newpassword, 'roles:list':'Manager'} http_result, result = call(acl_users.manage_users, submit="Change", **kw) def call(f, *args, **kw): # Call a function ignoring redirect bci errors. #return apply(f, args, kw) try: return apply(f,args, kw) except ServerError, v: if str(v)[:1] != '3': #raise "Anything", "else" raise sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2] _opid = os.path.isdir _opj = os.path.join def _findLibPath(): from glob import glob """ if you're lazy we'll have to find zope's python lib path ourselfs """ if _opid(_opj(os.path.abspath('.'), 'lib','python')): return _opj(os.path.abspath('.'), 'lib','python') elif glob('/usr/lib/zope*/lib/python'): findings = glob('/usr/lib/zope*/lib/python') if len(findings) > 1: findings.sort() # makes lowest first return findings[-1] else: return findings[0] return None # default if __name__=='__main__': main()