view MoinMoin/script/account/ @ 4926:7a8e1567e7e4

removed moin account check's --lastsaved option, it is default now (checking last use with trail file did not work in 1.9 anyway)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 01 Aug 2009 18:10:46 +0200
parents 2572688e031a
children a20de9383481
line wrap: on
line source
# -*- coding: iso-8859-1 -*-
MoinMoin - check / process user accounts

@copyright: 2003-2006 MoinMoin:ThomasWaldmann
@license: GNU GPL, see COPYING for details.

# ----------------------------------------------------------------------------
# if a user subscribes to magicpages, it means that he wants to keep
# exactly THIS account - this will avoid deleting it.
magicpages = [

# ----------------------------------------------------------------------------

import os, sys

from MoinMoin.script import MoinScript
from MoinMoin import user, wikiutil

class PluginScript(MoinScript):
When using ACLs, a wiki user name has to be unique, there must not be
multiple accounts having the same username. The problem is, that this
was possible before the introduction of ACLs and many users, who forgot
their ID, simply created a new ID using the same user name.

Because access rights (when using ACLs) depend on the NAME (not the ID),
this must be cleaned up before using ACLs or users will have difficulties
changing settings and saving their account data (system won't accept the
save, if the user name and email is not unique).

Detailed Instructions:

General syntax: moin [options] account check [check-options]

[options] usually should be:

[check-options] see below:

    0. Check the settings at top of the code!
       Making a backup of your wiki might be also a great idea.

    1. Best is to first look at duplicate user names:
       moin ... account check --usersunique

       If everything looks OK there, you may save that to disk:
       moin ... account check --usersunique --save

    2. Now, check also for duplicate email addresses:
       moin ... account check --emailsunique

       If everything looks OK, you may save that to disk:
       moin ... account check --emailsunique --save

    3. If the announced action is incorrect, you may choose to better manually
       disable some accounts: moin ... account disable --uid 1234567.8.90

    4. After cleaning up, do 1. and 2. again. There should be no output now, if
       everything is OK.

    5. Optionally you may want to make wikinames out of the user names
       moin ... account check --wikinames
       moin ... account check --wikinames --save

    def __init__(self, argv, def_values):
        MoinScript.__init__(self, argv, def_values)
            "Makes user names unique (by appending the ID to"
            " name and email, disabling subscribed pages and"
            " disabling all, but the latest saved user account);"
            " default is to SHOW what will be happening, you"
            " need to give the --save option to really do it."
            "Makes user emails unique;"
            " default is to show, use --save to save it."
            "Convert user account names to wikinames (camel-case)."
            "If specified as LAST option, will allow the other"
            " options to save user accounts back to disk."
            " If not specified, no settings will be changed permanently."
            "Remove pre-1.1 cleartext passwords from accounts."

    def _addFlag(self, name, help):
        self.parser.add_option("--" + name,
            action="store_true", dest=name, default=0, help=help)

    def collect_data(self):
        import re
        request = self.request
        for uid in user.getUserList(request):
            u = user.User(request, uid)
            self.users[uid] = u

            # collect name duplicates:
            if in self.names:
                self.names[] = [uid]

            # collect email duplicates:
                if in self.emails:
                    self.emails[] = [uid]

            # collect account with no or invalid email address set:
            if not or not re.match(".*@.*\..*",
                self.uids_noemail[uid] =

    def hasmagicpage(self, uid):
        u = self.users[uid]
        return u.isSubscribedTo(magicpages)

    def disableUser(self, uid):
        u = self.users[uid]
        print " %-20s %-30r %-35r" % (uid,,,
        keepthis = self.hasmagicpage(uid)
        if keepthis:
            print "- keeping (magicpage)!"
   # update timestamp, so this will be latest next run
        elif not u.disabled: # only disable once
            u.disabled = 1
   = "%s-%s" % (, uid)
       = "%s-%s" % (, uid)
            u.subscribed_pages = "" # avoid using email
                print "- disabled."
                print "- would be disabled."

    def getsortvalue(self, uid, user):
        return float(user.last_saved) # when user did last SAVE of his account data

    def process(self, uidlist):
        sortlist = []
        for uid in uidlist:
            u = self.users[uid]
            sortlist.append((self.getsortvalue(uid, u), uid))
        #print sortlist
        # disable all, but the last/latest one
        for dummy, uid in sortlist[:-1]:
        # show what will be kept
        uid = sortlist[-1][1]
        u = self.users[uid]
        print " %-20s %-30r %-35r - keeping%s!" % (uid,,, self.hasmagicpage(uid) and " (magicpage)" or "")

    def make_users_unique(self):
        for name, uids in self.names.items():
            if len(uids) > 1:

    def make_emails_unique(self):
        for email, uids in self.emails.items():
            if len(uids) > 1:

    def make_WikiNames(self):
        for uid, u in self.users.items():
            if u.disabled:
            if not wikiutil.isStrictWikiname(
                newname =" ", "").replace("-", "")
                if not wikiutil.isStrictWikiname(newname):
                    print " %-20s %-30r - no WikiName, giving up" % (uid,
                    print " %-20s %-30r - no WikiName -> %r" % (uid,, newname)
               = newname

    def remove_passwords(self):
        for uid, u in self.users.items():
            # user.User already clears the old cleartext passwords on loading,
            # so nothing to do here!
                # we can't encrypt the cleartext password as it is cleared
                # already. and we would not trust it anyway, so we don't WANT
                # to do that either!
                # Just save the account data without cleartext password:
                print " %-20s %-25s - saving" % (uid,

    def mainloop(self):
        # we don't expect non-option arguments
        if len(self.args) != 0:
            self.parser.error("incorrect number of arguments")

        # check for correct option combination
        flags_given = (self.options.usersunique
                    or self.options.emailsunique
                    or self.options.wikinames
                    or self.options.removepasswords)

        # no option given ==> show usage
        if not flags_given:


        self.users = {} # uid : UserObject
        self.names = {} # name : [uid, uid, uid]
        self.emails = {} # email : [uid, uid, uid]
        self.uids_noemail = {} # uid : name

        if self.options.usersunique:
        if self.options.emailsunique:
        if self.options.wikinames:
        if self.options.removepasswords: