Initial support to Group and Permission. The /admin can't be exec login in MongoDB yet. Only SQLsDB (SQLite,...)
This code work with django-mongoadmin pluggin.
This commit is contained in:
parent
6a4351e44f
commit
6a31736644
@ -3,6 +3,8 @@ import datetime
|
|||||||
from mongoengine import *
|
from mongoengine import *
|
||||||
|
|
||||||
from django.utils.encoding import smart_str
|
from django.utils.encoding import smart_str
|
||||||
|
from django.db import models
|
||||||
|
from django.contrib.contenttypes.models import ContentTypeManager
|
||||||
from django.contrib.auth.models import AnonymousUser
|
from django.contrib.auth.models import AnonymousUser
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
@ -34,6 +36,191 @@ except ImportError:
|
|||||||
|
|
||||||
REDIRECT_FIELD_NAME = 'next'
|
REDIRECT_FIELD_NAME = 'next'
|
||||||
|
|
||||||
|
class ContentType(Document):
|
||||||
|
name = StringField(max_length=100)
|
||||||
|
app_label = StringField(max_length=100)
|
||||||
|
model = StringField(max_length=100, verbose_name=_('python model class name'),
|
||||||
|
unique_with='app_label')
|
||||||
|
objects = ContentTypeManager()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
verbose_name = _('content type')
|
||||||
|
verbose_name_plural = _('content types')
|
||||||
|
# db_table = 'django_content_type'
|
||||||
|
# ordering = ('name',)
|
||||||
|
# unique_together = (('app_label', 'model'),)
|
||||||
|
|
||||||
|
def __unicode__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
def model_class(self):
|
||||||
|
"Returns the Python model class for this type of content."
|
||||||
|
from django.db import models
|
||||||
|
return models.get_model(self.app_label, self.model)
|
||||||
|
|
||||||
|
def get_object_for_this_type(self, **kwargs):
|
||||||
|
"""
|
||||||
|
Returns an object of this type for the keyword arguments given.
|
||||||
|
Basically, this is a proxy around this object_type's get_object() model
|
||||||
|
method. The ObjectNotExist exception, if thrown, will not be caught,
|
||||||
|
so code that calls this method should catch it.
|
||||||
|
"""
|
||||||
|
return self.model_class()._default_manager.using(self._state.db).get(**kwargs)
|
||||||
|
|
||||||
|
def natural_key(self):
|
||||||
|
return (self.app_label, self.model)
|
||||||
|
|
||||||
|
class SiteProfileNotAvailable(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class PermissionManager(models.Manager):
|
||||||
|
def get_by_natural_key(self, codename, app_label, model):
|
||||||
|
return self.get(
|
||||||
|
codename=codename,
|
||||||
|
content_type=ContentType.objects.get_by_natural_key(app_label, model)
|
||||||
|
)
|
||||||
|
|
||||||
|
class Permission(Document):
|
||||||
|
"""The permissions system provides a way to assign permissions to specific users and groups of users.
|
||||||
|
|
||||||
|
The permission system is used by the Django admin site, but may also be useful in your own code. The Django admin site uses permissions as follows:
|
||||||
|
|
||||||
|
- The "add" permission limits the user's ability to view the "add" form and add an object.
|
||||||
|
- The "change" permission limits a user's ability to view the change list, view the "change" form and change an object.
|
||||||
|
- The "delete" permission limits the ability to delete an object.
|
||||||
|
|
||||||
|
Permissions are set globally per type of object, not per specific object instance. It is possible to say "Mary may change news stories," but it's not currently possible to say "Mary may change news stories, but only the ones she created herself" or "Mary may only change news stories that have a certain status or publication date."
|
||||||
|
|
||||||
|
Three basic permissions -- add, change and delete -- are automatically created for each Django model.
|
||||||
|
"""
|
||||||
|
name = StringField(max_length=50, verbose_name=_('username'))
|
||||||
|
content_type = ReferenceField(ContentType)
|
||||||
|
codename = StringField(max_length=100, verbose_name=_('codename'))
|
||||||
|
# FIXME: don't access field of the other class
|
||||||
|
# unique_with=['content_type__app_label', 'content_type__model'])
|
||||||
|
|
||||||
|
objects = PermissionManager()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
verbose_name = _('permission')
|
||||||
|
verbose_name_plural = _('permissions')
|
||||||
|
# unique_together = (('content_type', 'codename'),)
|
||||||
|
# ordering = ('content_type__app_label', 'content_type__model', 'codename')
|
||||||
|
|
||||||
|
def __unicode__(self):
|
||||||
|
return u"%s | %s | %s" % (
|
||||||
|
unicode(self.content_type.app_label),
|
||||||
|
unicode(self.content_type),
|
||||||
|
unicode(self.name))
|
||||||
|
|
||||||
|
def natural_key(self):
|
||||||
|
return (self.codename,) + self.content_type.natural_key()
|
||||||
|
natural_key.dependencies = ['contenttypes.contenttype']
|
||||||
|
|
||||||
|
class Group(Document):
|
||||||
|
"""Groups are a generic way of categorizing users to apply permissions, or some other label, to those users. A user can belong to any number of groups.
|
||||||
|
|
||||||
|
A user in a group automatically has all the permissions granted to that group. For example, if the group Site editors has the permission can_edit_home_page, any user in that group will have that permission.
|
||||||
|
|
||||||
|
Beyond permissions, groups are a convenient way to categorize users to apply some label, or extended functionality, to them. For example, you could create a group 'Special users', and you could write code that would do special things to those users -- such as giving them access to a members-only portion of your site, or sending them members-only e-mail messages.
|
||||||
|
"""
|
||||||
|
name = StringField(max_length=80, unique=True, verbose_name=_('name'))
|
||||||
|
# permissions = models.ManyToManyField(Permission, verbose_name=_('permissions'), blank=True)
|
||||||
|
permissions = ListField(ReferenceField(Permission, verbose_name=_('permissions'), required=False))
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
verbose_name = _('group')
|
||||||
|
verbose_name_plural = _('groups')
|
||||||
|
|
||||||
|
def __unicode__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
class UserManager(models.Manager):
|
||||||
|
def create_user(self, username, email, password=None):
|
||||||
|
"""
|
||||||
|
Creates and saves a User with the given username, e-mail and password.
|
||||||
|
"""
|
||||||
|
now = datetime.datetime.now()
|
||||||
|
|
||||||
|
# Normalize the address by lowercasing the domain part of the email
|
||||||
|
# address.
|
||||||
|
try:
|
||||||
|
email_name, domain_part = email.strip().split('@', 1)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
email = '@'.join([email_name, domain_part.lower()])
|
||||||
|
|
||||||
|
user = self.model(username=username, email=email, is_staff=False,
|
||||||
|
is_active=True, is_superuser=False, last_login=now,
|
||||||
|
date_joined=now)
|
||||||
|
|
||||||
|
user.set_password(password)
|
||||||
|
user.save(using=self._db)
|
||||||
|
return user
|
||||||
|
|
||||||
|
def create_superuser(self, username, email, password):
|
||||||
|
u = self.create_user(username, email, password)
|
||||||
|
u.is_staff = True
|
||||||
|
u.is_active = True
|
||||||
|
u.is_superuser = True
|
||||||
|
u.save(using=self._db)
|
||||||
|
return u
|
||||||
|
|
||||||
|
def make_random_password(self, length=10, allowed_chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'):
|
||||||
|
"Generates a random password with the given length and given allowed_chars"
|
||||||
|
# Note that default value of allowed_chars does not have "I" or letters
|
||||||
|
# that look like it -- just to avoid confusion.
|
||||||
|
from random import choice
|
||||||
|
return ''.join([choice(allowed_chars) for i in range(length)])
|
||||||
|
|
||||||
|
|
||||||
|
# A few helper functions for common logic between User and AnonymousUser.
|
||||||
|
def _user_get_all_permissions(user, obj):
|
||||||
|
permissions = set()
|
||||||
|
anon = user.is_anonymous()
|
||||||
|
for backend in auth.get_backends():
|
||||||
|
if not anon or backend.supports_anonymous_user:
|
||||||
|
if hasattr(backend, "get_all_permissions"):
|
||||||
|
if obj is not None:
|
||||||
|
if backend.supports_object_permissions:
|
||||||
|
permissions.update(
|
||||||
|
backend.get_all_permissions(user, obj)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
permissions.update(backend.get_all_permissions(user))
|
||||||
|
return permissions
|
||||||
|
|
||||||
|
|
||||||
|
def _user_has_perm(user, perm, obj):
|
||||||
|
anon = user.is_anonymous()
|
||||||
|
active = user.is_active
|
||||||
|
for backend in auth.get_backends():
|
||||||
|
if (not active and not anon and backend.supports_inactive_user) or \
|
||||||
|
(not anon or backend.supports_anonymous_user):
|
||||||
|
if hasattr(backend, "has_perm"):
|
||||||
|
if obj is not None:
|
||||||
|
if (backend.supports_object_permissions and
|
||||||
|
backend.has_perm(user, perm, obj)):
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
if backend.has_perm(user, perm):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def _user_has_module_perms(user, app_label):
|
||||||
|
anon = user.is_anonymous()
|
||||||
|
active = user.is_active
|
||||||
|
for backend in auth.get_backends():
|
||||||
|
if (not active and not anon and backend.supports_inactive_user) or \
|
||||||
|
(not anon or backend.supports_anonymous_user):
|
||||||
|
if hasattr(backend, "has_module_perms"):
|
||||||
|
if backend.has_module_perms(user, app_label):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
class User(Document):
|
class User(Document):
|
||||||
"""A User document that aims to mirror most of the API specified by Django
|
"""A User document that aims to mirror most of the API specified by Django
|
||||||
at http://docs.djangoproject.com/en/dev/topics/auth/#users
|
at http://docs.djangoproject.com/en/dev/topics/auth/#users
|
||||||
|
Loading…
x
Reference in New Issue
Block a user