]>
git.ipfire.org Git - ipfire.org.git/blob - www/webapp/backend/accounts.py
8 from misc
import Singleton
9 from settings
import Settings
11 class Accounts(object):
12 __metaclass__
= Singleton
24 def search_base(self
):
25 return Settings().get("ldap_search_base")
30 ldap_uri
= self
.settings
.get("ldap_uri")
32 self
.__db
= ldap
.initialize(ldap_uri
)
34 bind_dn
= self
.settings
.get("ldap_bind_dn")
37 bind_pw
= self
.settings
.get("ldap_bind_pw")
39 self
.__db
.simple_bind(bind_dn
, bind_pw
)
44 return self
._accounts
[dn
]
48 results
= self
.db
.search_s(self
.search_base
, ldap
.SCOPE_SUBTREE
,
49 "(objectClass=posixAccount)", ["loginShell"])
51 for dn
, attrs
in results
:
52 #if attrs["loginShell"] == ["/bin/bash"]:
53 self
._accounts
[dn
] = Account(dn
)
56 return sorted(self
._accounts
.values())
59 for account
in self
.list():
60 if account
.uid
== uid
:
63 def delete(self
, uid
):
64 account
= self
.find(uid
)
70 class Account(object):
71 def __init__(self
, dn
):
74 self
.__attributes
= {}
77 return "<%s %s>" % (self
.__class
__.__name
__, self
.dn
)
79 def __cmp__(self
, other
):
80 return cmp(self
.cn
, other
.cn
)
88 if not self
.__attributes
:
89 self
.fetch_attributes()
91 return self
.__attributes
93 def fetch_attributes(self
):
94 result
= self
.db
.search_ext_s(self
.dn
, ldap
.SCOPE_SUBTREE
, sizelimit
=1)
95 dn
, self
.__attributes
= result
[0]
99 attribute
= self
.attributes
[key
]
101 raise AttributeError(key
)
103 if len(attribute
) == 1:
110 def set(self
, key
, value
):
111 mod_op
= ldap
.MOD_ADD
112 if self
.attributes
.has_key(key
):
113 mod_op
= ldap
.MOD_REPLACE
115 self
._modify
(mod_op
, key
, value
)
117 def _modify(self
, op
, key
, value
):
118 modlist
= [(op
, key
, value
)]
120 self
.db
.modify_s(self
.dn
, modlist
)
122 # Update local cache of attributes
123 self
.fetch_attributes()
125 def delete(self
, key
, value
=None):
126 self
._modify
(ldap
.MOD_DELETE
, key
, value
)
128 def check_password(self
, password
):
130 Bind to the server with given credentials and return
131 true if password is corrent and false if not.
133 Raises exceptions from the server on any other errors.
136 logging
.debug("Checking credentials for %s" % self
.dn
)
138 self
.db
.simple_bind_s(self
.dn
, password
)
139 except ldap
.INVALID_CREDENTIALS
:
146 return True # XXX todo
150 name
= self
.cn
.lower()
151 name
= name
.replace(" ", ".")
153 for mail
in self
.mail
:
154 if mail
.startswith(name
+ "@"):
157 raise Exception, "Cannot figure out email address"
159 def gravatar_icon(self
, size
=128):
161 gravatar_url
= "http://www.gravatar.com/avatar/" + \
162 hashlib
.md5(self
.email
.lower()).hexdigest() + "?"
163 gravatar_url
+= urllib
.urlencode({'d': "mm", 's': str(size
)})
167 if __name__
== "__main__":