def test_addUser(self): page = Add.addPage(self.avatarId, self.db, self.testSambaDomain) page.addSlash = False testutil.renderPage(page) submitData = { 'userSettings.uid': u'test01', 'userPermissions.employeeType': True, 'userPermissions.tumsUser': None, 'userSettings.userPassword': "******", 'mailSettings.vacation': None, 'mailSettings.mailAlternateAddress0': None, 'userPermissions.tumsAdmin': True, 'mailSettings.mailForwardingAddress0': None, 'userPermissions.accountStatus': True, 'userPermissions.employeeType': True, 'userSettings.sn': u'test', 'userSettings.givenName': u'test01', 'userAccess.ftpEnabled': False, 'userAccess.ftpGlobal': False, 'userAccess.vpnEnabled': False, 'userPermissions.copyto': '', } for i in range(10): submitData['mailSettings.mailAlternateAddress%s' % i] = None submitData['mailSettings.mailForwardingAddress%s' % i] = None form = None U = page.submitForm(self.aContext, form, submitData) U.addCallback(self._urlTest, page, '/Users/Edit/thusa.co.za/test01') return U
def submitForm(self, ctx, form, data): """Submit callback hook""" def buildAliasMap(fdata): """Builds a usable map of forwards and aliases""" mapsOutput = {} for line in fdata.readlines(): line = line.strip() if ':' in line: line = self.cleanupData.sub("", line) if len(line) > 0: #Something is left so lets see what we can do source, dest = line.split(":") destList = dest.split(",") if '@' in source: #Invalid entry continue for dest in destList: if not '@' in dest: dest += "@" + data['domain'] if source not in mapsOutput: mapsOutput[source] = [] mapsOutput[source].append(dest) return mapsOutput banUsers = ["root", "administrator", "nobody", "ftp"] #List of users we should rather ignore fdata = data['upload'][1] forwardMaps = buildAliasMap(fdata) adder = Add.addPage(self.avatarId, self.db, data['domain'].encode()) adder.domain = data['domain'].encode() userAddQueue = self.createUsersQueue(adder, ctx) for user in forwardMaps: if len(forwardMaps[user]) > 10: print "ERRRR: NO NO NO don't create an alias to anything more than 10 users use a mailing list skipping", user continue if user not in banUsers: self.setUser(ctx, userAddQueue, user, data["domain"], forwardMaps[user], data["createUser"]) if self.ldap: self.ldap.unbind_s() self.ldap = None userAddQueue.runQueue() return url.root.child('aliasImporter')
def thive_user(self, messageId, params, data): """ User record updated/new. if the user exists, initialise a call to Users.Edit.editPage otherwise pass to Users.Add.addPage Plain text password is passed from server down to client node. """ locator = params[0] # A resource locator (userid@domain) type = params[1] newdata = {} for kv in data.split('`'): if kv: k, v = kv.split(':', 1) newdata[k] = v tumsUser = [] squid = False admin = False for i in newdata['emp'].split('+'): if 'squid' in i: squid = True if 'tumsAdmin' in i: admin = True if 'tumsUser' in i: tumsUser = i.strip(']').split('[')[-1].split( ',') # list in brackets. flags = { 'vpn': False, 'ftpE': False, 'ftpG': False, 'copyto': u"", } flagsi = newdata['flags'].split('+') f = ['vpn', 'ftpE', 'ftpG'] for n, d in enumerate(flagsi[0]): flags[f[n]] = d == "-" if len(flagsi) > 1: flags['copyto'] = flagsi[1] submitData = { 'userPermissions.employeeType': squid, 'userPermissions.tumsUser': tumsUser, 'userPermissions.tumsAdmin': admin, 'userPermissions.accountStatus': newdata['active'] == "active", 'userSettings.uid': newdata['name'], 'userSettings.userPassword': newdata['password'], 'userSettings.sn': unicode(newdata['sn']), 'userSettings.givenName': unicode(newdata['giveName']), 'mailSettings.vacation': newdata['vacation'], 'mailSettings.vacen': newdata['vacEnable'] == "True", 'userPermissions.copyto': flags['copyto'], 'userAccess.vpnEnabled': flags['vpn'], 'userAccess.ftpEnabled': flags['ftpE'], 'userAccess.ftpGlobal': flags['ftpG'], } # Initialise all the psuedo-fields for i in range(10): submitData['mailSettings.mailAlternateAddress%s' % i] = u"" submitData['mailSettings.mailForwardingAddress%s' % i] = u"" # Teardown our real data for n, d in enumerate(newdata['mailAlias'].split('+')): submitData['mailSettings.mailAlternateAddress%s' % n] = unicode(d) for n, d in enumerate(newdata['mailForward'].split('+')): submitData['mailSettings.mailForwardingAddress%s' % n] = unicode(d) # Dummy form form = None #print submitData # decide which instance to create if type == "new": # Add new user (Not implemented on Thebe yet... addInstance = Add.addPage() p = addInstance.submitForm(None, None, submitData) else: #print "Edit ", locator # Alteration of existing user. try: db = (None, None, myLang, self) avatar = Auth.UserAvatar('root', '', 0, 0, True, [Settings.defaultDomain]) editInstance = Edit.editPage(avatar, db, locator.split('@')[0], newdata['domain']) p = editInstance.submitForm(None, None, submitData) except Exception, e: print e, "ERROR"
def submitForm(self, ctx, form, data): adder = Add.addPage(self.avatarId,self.db, Settings.defaultDomain) editor = Edit.editPage(self.avatarId, self.db, Settings.defaultDomain) deleter = Delete.deletePage(self.avatarId, self.db) groupSwitcher = Group.editGroups(self.avatarId, self.db) print data['upload'] # My crufty CSV parser. type, fdata,name = data['upload'] sepchar = None logs = [] lineno = 0 for l in fdata: lineno+= 1 # Clean it up l = l.strip('\n').strip() # continue if we don't have anything left if not l: continue # Figure out the separation character, we accept a few to play nice if not sepchar: sepChars = [",",";",":","\t"] sepcnt, char = (0, None) # Find the most occurances for i in sepChars: if l.count(i) > sepcnt: char = i sepchar = char print "Using sepchar ", repr(sepchar) prefields = l.split(sepchar) # Clean the fields # [type, uid, name, surname, password, perm1, perm2, groups ->] fields = ["A", "", "", "", "", "", ""] off = 0 for i,n in enumerate(prefields): st = n.strip(' ').strip("'").strip('"') if i == 0: if st not in ["A", "U", "D"]: off += 1 if len(fields) > (i+off): fields[i+off] = st else: if st: fields.append(st) action = fields[0] email = fields[5].lower() web = fields[6].lower() groups = fields[7:] groups.append('Domain Users') if email in ["yes", "true", "1"]: email = True else: email = False if web in ["yes", "true", "1"]: web = True else: web = False submitData = { 'userSettings.uid': unicode(fields[1].split('@')[0]), 'userPermissions.employeeType': web, 'userPermissions.tumsUser': None, 'userSettings.userPassword': fields[4] or "%s100" % fields[2], 'mailSettings.vacation': None, 'userPermissions.tumsAdmin': False, 'userPermissions.accountStatus': email, 'userSettings.sn': unicode(fields[3]), 'userSettings.givenName': unicode(fields[2]), 'userAccess.ftpEnabled': None, 'userAccess.ftpGlobal': None, 'userAccess.vpnEnabled': None, 'userPermissions.copyto': None } for i in range(10): submitData['mailSettings.mailAlternateAddress%s' % i] = u"" submitData['mailSettings.mailForwardingAddress%s' % i] = u"" # Figure out the domain if '@' in fields[1]: domain = fields[1].split('@')[-1] else: domain = Settings.defaultDomain print action, domain, submitData['userSettings.uid'], groups if action == "A" or action == "U": if action == "A": adder.domain = domain try: adder.submitForm(ctx, None, submitData) except: pass if action == "U": try: editor.domain = domain editor.cid = submitData['userSettings.uid'].encode("ascii", "replace") editor.submitForm(ctx, None, submitData) except: logs.append("Unable to modify '%s' on line %s. Check that the user exists" % ( submitData['userSettings.uid'].encode("ascii", "replace"), lineno) ) continue # If it's actualy a Samba domain.. if domain == Settings.defaultDomain: try: # Assemble a mapping with spaces stripped groupDict = {} for i in groups: groupDict[i.replace(' ', '')] = True groupSwitcher.domain = domain groupSwitcher.cid = submitData['userSettings.uid'].encode("ascii", "replace") # Update the permissions groupSwitcher.submitForm(ctx, None, groupDict) except: logs.append("Unable to modify memberships for '%s' on line %s. Check that the user exists" % ( submitData['userSettings.uid'].encode("ascii", "replace"), lineno) ) continue elif action == "D": deleter.locateChild(ctx, [domain, submitData['userSettings.uid']]) else: logs.append("Unable to understand command '%s' on line %s" % (action, lineno)) print logs return url.root.child('Users')