Beispiel #1
0
class Role(Model, RoleMixin):
    """
    A user's Role

    Attributes:

        * id -> Integer (Primary Key)
        * name -> String (Unique)
        * description -> String
    """
    id              = db.Column(db.Integer(), primary_key=True)
    name            = db.Column(db.String(80), unique=True)
    description     = db.Column(db.String(255))
Beispiel #2
0
class User(Model, UserMixin):
    """
    A user

    Attributes:

        * id -> Integer (Primary Key)
        * email -> String (Unique)
        * password -> String (Unique)
        * active -> Bool
        * confirmed_at -> DateTime
        * roles -> [Role]
    """
    id              = db.Column(db.Integer(), primary_key=True)
    email           = db.Column(db.String(255), unique=True)
    image           = db.Column(db.String(255), unique=True)
    name            = db.Column(db.String(255), unique=True)
    password        = db.Column(db.String(255))
    active          = db.Column(db.Boolean())
    confirmed_at    = db.Column(db.DateTime())
    auths           = db.relationship('Auth', backref='user', lazy='dynamic')
    roles           = db.relationship('Role', secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))
    watching        = db.relationship('Story', secondary=users_stories,
                            backref=db.backref('watchers', lazy='joined'))
    bookmarked      = db.relationship('Event', secondary=users_events,
                            backref=db.backref('bookmarkers', lazy='joined'))
    created_at      = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at      = db.Column(db.DateTime, default=datetime.utcnow)

    def __init__(self, auth=None, **kwargs):
        for key in kwargs:
            setattr(self, key, kwargs[key])

    def add_provider(self, provider, provider_id, access_token, access_token_secret=None, update=True):
        """
        Add a new provider authentication to this user.

        Raises an AuthExistsForUserException if this authentication
        already exists and is associated with another user.

        Args:
            | provider (str)            -- the provider name, e.g. 'twitter'
            | provider_id (str)         -- the id assigned by the provider
            | access_token (str)        -- the access token
            | access_token_secret (str) -- the access token secret
            | update (bool)             -- whether or not to update the existing
                                        provider authentication, if found (default: True)
        """
        # Check to see if this auth already exists.
        auth = Auth.for_provider(provider, provider_id)
        if auth:
            if auth.user is not self:
                raise AuthExistsForUserException('Found an existing authorization for {0} associated with another user.'.format(provider))
            elif update:
                auth.update_token(access_token, access_token_secret)
        else:
            auth = Auth(provider, provider_id, access_token, access_token_secret)
            auth.user = self
            db.session.add(auth)

        db.session.commit()
        return auth

    def merge(self, user):
        """
        Merge this user with another user,
        where *this* user is considered the canonical
        user (i.e. its attributes are preferred over
        the other user's).

        UI tip: prompt the user to pick which account is their primary one!
        """
        providers = [auth.provider for auth in self.auths]
        for auth in user.auths:
            # In the event that the merged user has authentications
            # which conflict with one on this user, prefer the one on this user.
            # I don't anticipate this will happen, but it's possible, e.g. if a user
            # has two twitter accts and authenticates each on different user accts here.
            if auth.provider not in providers:
                auth.user = self
        db.session.delete(user)
        db.session.commit()

    @staticmethod
    def for_provider(provider, provider_id):
        """
        Find an User instance by provider.

        Args:
            | provider (str)        -- the provider name, e.g. 'twitter'
            | provider_id (str)     -- the user id assigned by the provider
        """
        auth = Auth.for_provider(provider, provider_id)
        if auth:
            return auth.user
        return None
Beispiel #3
0
from datetime import datetime
from Crypto.Cipher import AES

from argos.datastore import db, Model

from flask import current_app
from flask.ext.security import Security, UserMixin, RoleMixin

# Table connecting users and roles
roles_users = db.Table('roles_users',
        db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
        db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))

# Table for users watching stories.
users_stories = db.Table('users_stories',
        db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
        db.Column('story_id', db.Integer(), db.ForeignKey('story.id')))

# Table for users bookmarking events.
users_events = db.Table('users_events',
        db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
        db.Column('event_id', db.Integer(), db.ForeignKey('event.id')))

# Table for users 

class AuthExistsForUserException(Exception):
    pass

class Role(Model, RoleMixin):
    """
    A user's Role