]>
git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - webapp/backend/accounts.py
9 from misc
import Object
11 class Accounts(Object
):
12 def __init__(self
, backend
):
13 Object
.__init
__(self
, backend
)
19 def search_base(self
):
20 return self
.settings
.get("ldap_search_base")
25 ldap_uri
= self
.settings
.get("ldap_uri")
27 self
.__db
= ldap
.initialize(ldap_uri
)
29 bind_dn
= self
.settings
.get("ldap_bind_dn")
32 bind_pw
= self
.settings
.get("ldap_bind_pw", "")
34 self
.__db
.simple_bind(bind_dn
, bind_pw
)
39 return self
._accounts
[dn
]
43 results
= self
.db
.search_s(self
.search_base
, ldap
.SCOPE_SUBTREE
,
44 "(objectClass=posixAccount)", ["loginShell"])
46 for dn
, attrs
in results
:
47 #if attrs["loginShell"] == ["/bin/bash"]:
48 self
._accounts
[dn
] = Account(self
.backend
, dn
)
51 return sorted(self
._accounts
.values())
54 for account
in self
.list():
55 if account
.uid
== uid
:
58 def delete(self
, uid
):
59 account
= self
.find(uid
)
65 class Account(Object
):
66 def __init__(self
, backend
, dn
):
67 Object
.__init
__(self
, backend
)
70 self
.__attributes
= {}
73 return "<%s %s>" % (self
.__class
__.__name
__, self
.dn
)
75 def __cmp__(self
, other
):
76 return cmp(self
.cn
, other
.cn
)
80 return self
.accounts
.db
84 if not self
.__attributes
:
85 self
.fetch_attributes()
87 return self
.__attributes
89 def fetch_attributes(self
):
90 result
= self
.db
.search_ext_s(self
.dn
, ldap
.SCOPE_SUBTREE
, sizelimit
=1)
91 dn
, self
.__attributes
= result
[0]
95 attribute
= self
.attributes
[key
]
97 raise AttributeError(key
)
99 if len(attribute
) == 1:
106 def set(self
, key
, value
):
107 mod_op
= ldap
.MOD_ADD
108 if self
.attributes
.has_key(key
):
109 mod_op
= ldap
.MOD_REPLACE
111 self
._modify
(mod_op
, key
, value
)
113 def _modify(self
, op
, key
, value
):
114 modlist
= [(op
, key
, value
)]
116 self
.db
.modify_s(self
.dn
, modlist
)
118 # Update local cache of attributes
119 self
.fetch_attributes()
121 def delete(self
, key
, value
=None):
122 self
._modify
(ldap
.MOD_DELETE
, key
, value
)
124 def check_password(self
, password
):
126 Bind to the server with given credentials and return
127 true if password is corrent and false if not.
129 Raises exceptions from the server on any other errors.
132 logging
.debug("Checking credentials for %s" % self
.dn
)
134 self
.db
.simple_bind_s(self
.dn
, password
.encode("utf-8"))
135 except ldap
.INVALID_CREDENTIALS
:
136 logging
.debug("Account credentials are invalid.")
139 logging
.debug("Successfully authenticated.")
144 return True # XXX todo
152 name
= self
.cn
.lower()
153 name
= name
.replace(" ", ".")
154 name
= name
.replace("Ä", "Ae")
155 name
= name
.replace("Ö", "Oe")
156 name
= name
.replace("Ü", "Ue")
157 name
= name
.replace("ä", "ae")
158 name
= name
.replace("ö", "oe")
159 name
= name
.replace("ü", "ue")
161 for mail
in self
.mail
:
162 if mail
.startswith("%s@ipfire.org" % name
):
165 raise Exception, "Cannot figure out email address"
167 def gravatar_icon(self
, size
=128):
169 gravatar_url
= "http://www.gravatar.com/avatar/" + \
170 hashlib
.md5(self
.email
.lower()).hexdigest() + "?"
171 gravatar_url
+= urllib
.urlencode({'d': "mm", 's': str(size
)})
176 if __name__
== "__main__":