Attachment 'disableusers.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - disable users
   4 
   5     @copyright: 2001-2004 Juergen Hermann <jh@web.de>,
   6                 2003-2007 MoinMoin:ThomasWaldmann
   7                 2007      MoinMoin:JohannesBerg
   8     @license: GNU GPL, see COPYING for details.
   9 """
  10 
  11 from MoinMoin import user, util, wikiutil
  12 from MoinMoin.widget import html
  13 from MoinMoin.userprefs import UserPrefBase
  14 
  15 
  16 class Settings(UserPrefBase):
  17 
  18     def __init__(self, request):
  19         """ Initialize disable users form. """
  20         UserPrefBase.__init__(self, request)
  21         self.request = request
  22         self._ = request.getText
  23         self.cfg = request.cfg
  24         _ = self._
  25         self.title = _("Disable users")
  26         self.name = 'disableusers'
  27 
  28     def allowed(self):
  29         return (self.request.user.auth_method in self.request.cfg.auth_can_logout and
  30                UserPrefBase.allowed(self) and self.request.user.isSuperUser())
  31 
  32     def handle_form(self):
  33         _ = self._
  34         form = self.request.form
  35 
  36         if 'cancel' in form:
  37             return None
  38 
  39         if (wikiutil.checkTicket(self.request, self.request.form['ticket'])
  40             and self.request.method == 'POST'):
  41             names = []
  42             for uid in form.getlist('selected_users'):
  43                 if not uid:
  44                     return 'error', _("No user selected")
  45                 theuser = user.User(self.request, uid, auth_method='setuid')
  46                 if not theuser or not theuser.exists():
  47                     return 'error', _("No user selected")
  48                 theuser.disabled = 1
  49                 names.append(theuser.name)
  50                 theuser.save()
  51             return  _("Users %r disabled." % names)
  52         else:
  53             return None
  54 
  55     def _user_select(self):
  56         options = []
  57         users = user.getUserList(self.request)
  58         current_uid = self.request.user.id
  59         for uid in users:
  60             if uid != current_uid:
  61                 u = user.User(self.request, id=uid)
  62                 if u.disabled:
  63                     continue
  64                 name = u.name
  65                 options.append((uid, name))
  66         options.sort(lambda x, y: cmp(x[1].lower(), y[1].lower()))
  67 
  68         if not options:
  69             _ = self._
  70             self._only = True
  71             return _("You are the only user.")
  72 
  73         self._only = False
  74         size = min(20, len(options))
  75         return util.web.makeSelection('selected_users', options, current_uid, size=size,
  76                                       multiple=True)
  77 
  78     def create_form(self):
  79         """ Create the complete HTML form code. """
  80         _ = self._
  81         form = self.make_form(html.Text(_('As a superuser, you can permanently disable other accounts.')))
  82 
  83         ticket = wikiutil.createTicket(self.request)
  84         self.make_row(_('Select Users'), [self._user_select()], valign="top")
  85         form.append(html.INPUT(type="hidden", name="ticket", value="%s" % ticket))
  86         if not self._only:
  87             buttons = [html.INPUT(type="submit", name="select_user",
  88                                   value=_('Select User')),
  89                        ' ', ]
  90         else:
  91             buttons = []
  92         buttons.append(html.INPUT(type="submit", name="cancel",
  93                                   value=_('Cancel')))
  94         self.make_row('', buttons)
  95         return unicode(form)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2011-10-10 11:58:01, 3.3 KB) [[attachment:disableusers.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.