Shabti / shabti / templates / auth / +package+ / model / identity.py_tmpl

{{if sqlalchemy}}
from datetime import datetime
from elixir import *
from elixir import events
import hashlib
from {{package}}.model import Session, metadata

options_defaults['inheritance'] = 'multi'

def encrypt_value(value):
    return hashlib.sha1(value).hexdigest()

class NotAuthenticated(Exception):pass

class User(Entity):
    """User"""
    username = Field(Unicode(30), unique=True) # undocumented
    password = Field(String(40)) # undocumented
    password_check = Field(String(40)) # undocumented
    email = Field(String(255)) # undocumented
    created = Field(DateTime) # undocumented
    active = Field(Boolean) # undocumented
    groups = ManyToMany('Group')
    using_options(shortnames=True)
    
    def __repr__(self):
        return '<%r %r, email: %r, created: %s, active: %s>' \
               % (self.__class__.__name__.capitalize(), self.username, 
                  self.email, self.created, self.active)
    
    def __unicode__(self):
        return self.username
    
    @classmethod
    def authenticate(cls, username, password):
        try:
            user=cls.query.filter_by(username=username, active=True).one()
            if user and encrypt_value(password) == user.password:
                return user
        except Exception:
            raise NotAuthenticated
        raise NotAuthenticated
    
    def validate_password(user, password):
        return encrypt_value(password) == user.password
    
    @events.before_insert
    @events.before_update
    def encrypt_password(self):
        if self.password and self.password != self.password_check:
            self.password = encrypt_value(self.password)
            self.password_check = self.password
    
    @property
    def permissions(self):
        permissions = set()
        for g in self.groups:
            permissions = permissions | set(g.permissions)
        return permissions        
    
    @property
    def permission_names(self):
        return [p.name for p in self.permissions]
    
    def has_permission(self, perm):
        return (perm in self.permission_names)
    


class Group(Entity):
    name = Field(Unicode(30)) # undocumented
    description = Field(Unicode(255)) # undocumented
    created = Field(DateTime) # undocumented
    active = Field(Boolean) # undocumented
    users = ManyToMany('User')
    permissions = ManyToMany('Permission')
    using_options(shortnames=True)
    
    def __repr__(self):
        return '<%r %r, description: %r, created: %s, active: %s>' \
               % (self.__class__.__name__.capitalize(), self.name, 
                  self.description, self.created.ctime(), self.active)
    
    def __unicode__(self):
        return self.name

class Permission(Entity):
    name = Field(Unicode(30)) # undocumented
    description = Field(Unicode(255)) # undocumented
    groups = ManyToMany('Group', onupdate = 'CASCADE', 
                        ondelete = 'CASCADE', uselist = True)
    using_options(shortnames=True)
    
    def __repr__(self):
        return '<%r %r, description: %r>' \
               % (self.__class__.__name__.capitalize(), 
                  self.name, self.description)
    
    def __unicode__(self):
        return self.name


__all__=['User', 'Permission', 'Group', 'NotAuthenticated']

# --- Added by Shabti auth template
{{endif}}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.