from storage import StorableObject

class User(StorableObject):
    _names = 'users'

    name = None
    email = None
    roles = None
    name_identifiers = None
    identification_token = None
#    lasso_dump = None
    lasso_dumps = None
    is_admin = False
    anonymous = False

    def __init__(self, name=None):
        StorableObject.__init__(self)
        self.name = name
        self.name_identifiers = []
        self.roles = []
        self.lasso_dumps = []

    def migrate(self):
        changed = False

        if self.roles and 'site-admin' in self.roles:
            self.is_admin = True
            self.roles = [x for x in self.roles if x != 'site-admin']
            changed = True

        if changed:
            self.store()

    def remove_name_identifier(self, provider_id, name_identifier):
        self.name_identifiers.remove(name_identifier)
        if not self.name_identifiers:
            self.remove_self()
        else:
            self.store()

    def __str__(self):
        return 'User %s, name : %s, name identifiers : %s, lasso_dumps : %s, token : %s' \
                % (self.id, self.name, self.name_identifiers, self.lasso_dumps, self.identification_token)
