def addUser(self, user, suppress_welcome=False): if shell.is_pretend(): suppress_welcome = True logger.info("Pretending to add {0} to site {1}".format(user, self)) session = db.Session() logger.debug('Users in {0}: {1}'.format(self.shortname, self.users)) if user in self.users: raise Site.AlreadyHasUser if not shell.is_pretend(): self.users.append(user) session.commit() try: do("gpasswd -a {0} {1}".format(user.username, self.shortname)) do("ln -s {0} {1}".format(self._get_home(), shell.join(user._get_home(), self.shortname))) except shell.ShellActionFailed: if not shell.is_pretend(): self.users.remove(user) session.commit() raise else: if not suppress_welcome: email_vars = { "$FULL_NAME": user.full_name, "$SITE_SHORTNAME": self.shortname, } email_message = shell.format(open(shell.join(config.TEMPLATE_ROOT, 'site_adduser_email.txt')).read(), email_vars) email_subject = "Peninsula Account Update: {0} added to site {1}".format(user.username, self.shortname) user.send_email(email_subject, email_message) logger.info("Sent adduser email to {0}".format(user.email)) logger.info('Added {0} to {1}'.format(user.username, self.shortname))
def create(username, full_name, email, suppress_welcome=False, fake_create=False): if fake_create: suppress_welcome = True session = db.Session() if not config.NAME_REGEX.match(username) or len(username) > config.NAME_LIMIT: raise User.BadName("{0} is not a valid username (containing only letters, numbers, and dashes and being between 2 and {1} characters)".format(username, config.NAME_LIMIT)) if User.get(username): raise User.Exists("There is already a user named {0} in piccolo".format(username)) try: pwd.getpwnam(username) except KeyError: pass else: if not fake_create: raise User.Exists("There is already a user named {0} in /etc/passwd".format(username)) if shell.exists(shell.join(config.USERS_ROOT, username)) and not fake_create: raise User.BadName("Cannot create user {1} because folder {0} already exists.".format(shell.join(config.USERS_ROOT, username), username)) logger.debug("user doesn't exist yet") new_user = User(username, full_name, email) session.add(new_user) session.commit() new_user._temp_password = shell.generate_password(length=12) try: if not fake_create: new_user._shell_create() except User.ShellActionFailed: session.delete(new_user) session.commit() raise else: logger.info('Created user "{0}" [{1}] with contact email <{2}>'.format(full_name, username, email)) if not suppress_welcome: user_vars = new_user._vars() user_vars.update({"$INITIAL_PASSWORD": new_user._temp_password,}) email_message = shell.format(open(shell.join(config.TEMPLATE_ROOT, 'user_email.txt')).read(), user_vars) email_subject = "New Peninsula Account {0}".format(new_user.username) new_user.send_email(email_subject, email_message) logger.info("Sent welcome email to {0}".format(new_user.email)) elif not fake_create: logger.info("User's initial password: {0}".format(new_user._temp_password))
def _format_template(self, src): src = shell.join(config.TEMPLATE_ROOT, src) return shell.format(open(src, 'r').read(), self._vars())