summaryrefslogtreecommitdiffstats
path: root/accounts/models.py
blob: 9e0c45ed495b4e2de30a123d891b599281f2ee05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# -*- coding: utf-8 -*-

class Account(object):
    """
    An Account represents a complex ldap tree entry for spline users.
    For each service a spline user can have a different password.
    """

    def __init__(self, uid, mail, services=[], dn=None, password=None):
        self.uid = uid.encode('utf8') if isinstance(uid, unicode) else uid
        self.services = services
        self.dn = dn
        self.password = password.encode('utf8') if isinstance(password, unicode) else password
        self.new_password_root = None
        self.new_password_services = {}
        self.attributes = {}

        self._set_attribute('mail', mail)

    def __repr__(self):
        return "<Account uid=%s>" % self.uid


    def reset_password(self, service):
         self.new_password_services[service] = (None, None)

    def change_password(self, new_password, old_password='', service=None):
        """
        Changes a password for a given service. You have to use the
        AccountService class to make the changes permanent. If no service is
        given, the root password will be changed.
        """

        if isinstance(new_password, unicode):
            new_password = new_password.encode('utf8')

        if isinstance(old_password, unicode):
            old_password = old_password.encode('utf8')

        if not service:
            self.new_password_root = (old_password, new_password)
        else:
            self.new_password_services[service] = (old_password, new_password)

    def _set_attribute(self, key, value):
        if isinstance(value, unicode):
            value = value.encode('utf8')

        self.attributes[key] = value

    def change_email(self, new_mail):
        """
        Changes the mail address of an account. You have to use the
        AccountService class to make changes permanent.
        """
        self._set_attribute('mail', new_mail)

    def __getattr__(self, name):
        if name in self.attributes:
            return self.attributes[name]

        raise AttributeError("'%s' object has no attribute '%s'" %
                             (self.__class__.__name__, name))


class Service(object):
    def __init__(self, id, name, url):
        self.id = id
        self.name = name
        self.url = url
        self.changed = None # used by settings view

    def __repr__(self):
        return '<Service %s>' % self.id