view MoinMoin/script/account/ @ 1974:87272c032485

remove unused imports
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 10 Feb 2013 23:54:13 +0100
parents a2d2f4a93e0b
children 384555088cab
line wrap: on
line source
# Copyright: 2006-2013 MoinMoin:ThomasWaldmann
# Copyright: 2008 MoinMoin:JohannesBerg
# Copyright: 2011 MoinMoin:ReimarBauer
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

MoinMoin - set a user password

from flask.ext.script import Command, Option

from MoinMoin.constants.keys import ITEMID, NAME, NAME_EXACT, EMAIL
from MoinMoin import user
from import before_wiki

class Fault(Exception):
    """something went wrong"""

class NoSuchUser(Fault):
    """raised if no such user exists"""

class MailFailed(Fault):
    """raised if e-mail sending failed"""

def set_password(uid, password, notify=False):
    u = user.User(uid)
    if u and u.exists():
        if notify and not u.disabled and
            mailok, msg = u.mail_password_recovery()
            if not mailok:
                raise MailFailed(msg)
        raise NoSuchUser('User does not exist (name: %r id: %r)!' % (,

class Set_Password(Command):
    description = 'This command allows you to set a user password.'
    option_list = (
        Option('--name', '-n', required=False, dest='name', type=unicode,
               help='Set password for the user with user name NAME.'),
        Option('--uid', '-u', required=False, dest='uid', type=unicode,
               help='Set password for the user with user id UID.'),
        Option('--password', '-p', required=False, dest='password', type=unicode,
               help='New password for this account.'),
        Option('--all-users', '-a', required=False, dest='all_users', action='store_true', default=False,
            help='Reset password for ALL users.'),
        Option('--notify', '-N', required=False, dest='notify', action='store_true', default=False,
            help='Notify user(s), send them an E-Mail with a password reset link.'),
        Option('--verbose', '-v', required=False, dest='verbose', action='store_true', default=False,
            help='Verbose operation'),

    def run(self, name, uid, password, all_users, notify, verbose):
        flags_given = name or uid or all_users
        if not flags_given:
            print 'incorrect number of arguments'
            import sys

        if uid:
            query = {ITEMID: uid}
        elif name:
            query = {NAME_EXACT: name}
        elif all_users:
            query = {}

        # sorting the list so we have some specific, reproducable order
        uids_metas = sorted([(rev.meta[ITEMID], rev.meta) for rev in user.search_users(**query)])
        total = len(uids_metas)
        for nr, (uid, meta) in enumerate(uids_metas, start=1):
            name = meta[NAME]
            email = meta[EMAIL]
                set_password(uid, password, notify=notify)
            except Fault, err:
                status = "FAILURE: [%s]" % str(err)
                status = "SUCCESS"
            if verbose:
                print "uid %s, name %s, email %s (%05d / %05d) %s" % (uid, name, email, nr, total, status)