Ticket #6273: django_auth_passwd_command.diff

File django_auth_passwd_command.diff, 6.7 KB (added by Ludvig Ericson, 17 years ago)
  • django/contrib/auth/management.py

     
    1 """
    2 Creates permissions for all installed apps that need permissions.
    3 """
    4 
    5 from django.dispatch import dispatcher
    6 from django.db.models import get_models, signals
    7 from django.contrib.auth import models as auth_app
    8 
    9 def _get_permission_codename(action, opts):
    10     return u'%s_%s' % (action, opts.object_name.lower())
    11 
    12 def _get_all_permissions(opts):
    13     "Returns (codename, name) for all permissions in the given opts."
    14     perms = []
    15     for action in ('add', 'change', 'delete'):
    16         perms.append((_get_permission_codename(action, opts), u'Can %s %s' % (action, opts.verbose_name_raw)))
    17     return perms + list(opts.permissions)
    18 
    19 def create_permissions(app, created_models, verbosity):
    20     from django.contrib.contenttypes.models import ContentType
    21     from django.contrib.auth.models import Permission
    22     app_models = get_models(app)
    23     if not app_models:
    24         return
    25     for klass in app_models:
    26         ctype = ContentType.objects.get_for_model(klass)
    27         for codename, name in _get_all_permissions(klass._meta):
    28             p, created = Permission.objects.get_or_create(codename=codename, content_type__pk=ctype.id,
    29                 defaults={'name': name, 'content_type': ctype})
    30             if created and verbosity >= 2:
    31                 print "Adding permission '%s'" % p
    32 
    33 def create_superuser(app, created_models, verbosity, **kwargs):
    34     from django.contrib.auth.models import User
    35     from django.contrib.auth.create_superuser import createsuperuser as do_create
    36     if User in created_models and kwargs.get('interactive', True):
    37         msg = "\nYou just installed Django's auth system, which means you don't have " \
    38                 "any superusers defined.\nWould you like to create one now? (yes/no): "
    39         confirm = raw_input(msg)
    40         while 1:
    41             if confirm not in ('yes', 'no'):
    42                 confirm = raw_input('Please enter either "yes" or "no": ')
    43                 continue
    44             if confirm == 'yes':
    45                 do_create()
    46             break
    47 
    48 dispatcher.connect(create_permissions, signal=signals.post_syncdb)
    49 dispatcher.connect(create_superuser, sender=auth_app, signal=signals.post_syncdb)
  • django/contrib/auth/management/commands/__init__.py

     
     1"""Placeholder file to make directory a package."""
  • django/contrib/auth/management/commands/passwd.py

     
     1from django.core.management.base import BaseCommand, CommandError
     2from django.contrib.auth.models import User
     3import getpass
     4
     5class Command(BaseCommand):
     6    help = "Clone of the UNIX program ``passwd'', for django.contrib.auth."
     7
     8    requires_model_validation = False
     9
     10    def _get_pass(self, prompt="Password: "):
     11        p = getpass.getpass(prompt=prompt)
     12        if not p:
     13            raise CommandError("aborted")
     14        return p
     15
     16    def handle(self, *args, **options):
     17        if len(args) > 1:
     18            raise CommandError("need exactly one or zero arguments for username")
     19
     20        if args:
     21            username, = args
     22        else:
     23            username = getpass.getuser()
     24
     25        try:
     26            u = User.objects.get(username=username)
     27        except User.DoesNotExist:
     28            raise CommandError("user %s does not exist" % username)
     29
     30        print "Changing password for user", u.username
     31        p1, p2 = 1, 2  # To make them mismatch.
     32        while p1 != p2:
     33            p1 = self._get_pass()
     34            p2 = self._get_pass("Password (again): ")
     35            if p1 != p2:
     36                print "Passwords do not match, try again"
     37
     38        u.set_password(p1)
     39        u.save()
     40
     41        return "Password changed successfully for user " + u.username
  • django/contrib/auth/management/__init__.py

     
     1"""
     2Creates permissions for all installed apps that need permissions.
     3"""
     4
     5from django.dispatch import dispatcher
     6from django.db.models import get_models, signals
     7from django.contrib.auth import models as auth_app
     8
     9def _get_permission_codename(action, opts):
     10    return u'%s_%s' % (action, opts.object_name.lower())
     11
     12def _get_all_permissions(opts):
     13    "Returns (codename, name) for all permissions in the given opts."
     14    perms = []
     15    for action in ('add', 'change', 'delete'):
     16        perms.append((_get_permission_codename(action, opts), u'Can %s %s' % (action, opts.verbose_name_raw)))
     17    return perms + list(opts.permissions)
     18
     19def create_permissions(app, created_models, verbosity):
     20    from django.contrib.contenttypes.models import ContentType
     21    from django.contrib.auth.models import Permission
     22    app_models = get_models(app)
     23    if not app_models:
     24        return
     25    for klass in app_models:
     26        ctype = ContentType.objects.get_for_model(klass)
     27        for codename, name in _get_all_permissions(klass._meta):
     28            p, created = Permission.objects.get_or_create(codename=codename, content_type__pk=ctype.id,
     29                defaults={'name': name, 'content_type': ctype})
     30            if created and verbosity >= 2:
     31                print "Adding permission '%s'" % p
     32
     33def create_superuser(app, created_models, verbosity, **kwargs):
     34    from django.contrib.auth.models import User
     35    from django.contrib.auth.create_superuser import createsuperuser as do_create
     36    if User in created_models and kwargs.get('interactive', True):
     37        msg = "\nYou just installed Django's auth system, which means you don't have " \
     38                "any superusers defined.\nWould you like to create one now? (yes/no): "
     39        confirm = raw_input(msg)
     40        while 1:
     41            if confirm not in ('yes', 'no'):
     42                confirm = raw_input('Please enter either "yes" or "no": ')
     43                continue
     44            if confirm == 'yes':
     45                do_create()
     46            break
     47
     48dispatcher.connect(create_permissions, signal=signals.post_syncdb)
     49dispatcher.connect(create_superuser, sender=auth_app, signal=signals.post_syncdb)
Back to Top