def add_users(self, branch, usersfile): if self.config.readonly: yield error_log('This LDAP is configured as read-only') self.set_branch(branch) self.connect(auth=False) self.authenticate( username=self.effective_admin_dn, password=self.effective_admin_password) try: users = read_users_file(usersfile, required_fields=['username', 'fullname', 'password']) except Exception as exc: error_message = 'Error parsing users file {}: {{}}'.format(usersfile) yield raising_error_log(error_message.format(exc.message)) try: self.check_users(users) except Exception as exc: yield raising_error_log(exc.message) yield step_log('Creating {} users '.format(len(users))) for count, user in enumerate(users, start=1): if not user: yield error_log('Error parsing user at line #{}'.format(count)) continue try: self.add_ldap_user(**user) yield success_log('User {} created'.format(user['username'])) except ldap.ALREADY_EXISTS: yield error_log('User {} already exists'.format(user['username'])) except Exception as exc: yield error_log('Error creating user {}: {}'.format(user['username']), exc.__repr__()) self.disconnect()
def batch_add_users(self, instance, usersfile): site = UlearnSite(self.get_environment(instance["environment"]), instance["mountpoint"], instance["plonesite"]) try: users = read_users_file(usersfile, required_fields=["username", "fullname", "email", "password"]) except Exception as exc: error_message = "Error parsing users file {}: {{}}".format(usersfile) yield raising_error_log(error_message.format(exc.message)) try: self.check_users(users) except Exception as exc: yield raising_error_log(exc.message) yield step_log("Creating {} users ".format(len(users))) for count, user in enumerate(users, start=1): if not user: yield error_log("Error parsing user at line #{}".format(count)) continue succeeded = site.add_user(**user) if not succeeded.get("error", False): yield success_log(succeeded["message"]) else: yield error_log(succeeded["message"])