1 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
1 # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
3 # |
3 # |
4 # This file is part of CubicWeb. |
4 # This file is part of CubicWeb. |
5 # |
5 # |
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
18 """Define server side service provided by cubicweb""" |
18 """Define server side service provided by cubicweb""" |
19 |
19 |
20 import threading |
20 import threading |
21 |
21 |
|
22 from yams.schema import role_name |
|
23 from cubicweb import ValidationError |
22 from cubicweb.server import Service |
24 from cubicweb.server import Service |
23 from cubicweb.predicates import match_user_groups |
25 from cubicweb.predicates import match_user_groups, match_kwargs |
24 |
26 |
25 class StatsService(Service): |
27 class StatsService(Service): |
26 """Return a dictionary containing some statistics about the repository |
28 """Return a dictionary containing some statistics about the repository |
27 resources usage. |
29 resources usage. |
28 """ |
30 """ |
98 results['lookupclasses'] = values |
100 results['lookupclasses'] = values |
99 values = sorted(ocounters.iteritems(), key=lambda x: x[1], reverse=True)[:nmax] |
101 values = sorted(ocounters.iteritems(), key=lambda x: x[1], reverse=True)[:nmax] |
100 results['referenced'] = values |
102 results['referenced'] = values |
101 results['unreachable'] = len(garbage) |
103 results['unreachable'] = len(garbage) |
102 return results |
104 return results |
|
105 |
|
106 |
|
107 class RegisterUserService(Service): |
|
108 """check if a user with the given login exists, if not create it with the |
|
109 given password. This service is designed to be used for anonymous |
|
110 registration on public web sites. |
|
111 |
|
112 To use it, do: |
|
113 with self.appli.repo.internal_cnx() as cnx: |
|
114 cnx.call_service('register_user', |
|
115 login=login, |
|
116 password=password, |
|
117 **kwargs) |
|
118 """ |
|
119 __regid__ = 'register_user' |
|
120 __select__ = Service.__select__ & match_kwargs('login', 'password') |
|
121 |
|
122 def call(self, login, password, email=None, **kwargs): |
|
123 cnx = self._cw |
|
124 errmsg = cnx._('the value "%s" is already used, use another one') |
|
125 |
|
126 if (cnx.execute('CWUser X WHERE X login %(login)s', {'login': login}, |
|
127 build_descr=False) |
|
128 or cnx.execute('CWUser X WHERE X use_email C, C address %(login)s', |
|
129 {'login': login}, build_descr=False)): |
|
130 qname = role_name('login', 'subject') |
|
131 raise ValidationError(None, {qname: errmsg % login}) |
|
132 |
|
133 if isinstance(password, unicode): |
|
134 # password should *always* be utf8 encoded |
|
135 password = password.encode('UTF8') |
|
136 kwargs['login'] = login |
|
137 kwargs['upassword'] = password |
|
138 # we have to create the user |
|
139 user = cnx.create_entity('CWUser', **kwargs) |
|
140 cnx.execute('SET X in_group G WHERE X eid %(x)s, G name "users"', |
|
141 {'x': user.eid}) |
|
142 |
|
143 if email or '@' in login: |
|
144 d = {'login': login, 'email': email or login} |
|
145 if cnx.execute('EmailAddress X WHERE X address %(email)s', d, |
|
146 build_descr=False): |
|
147 qname = role_name('address', 'subject') |
|
148 raise ValidationError(None, {qname: errmsg % d['email']}) |
|
149 cnx.execute('INSERT EmailAddress X: X address %(email)s, ' |
|
150 'U primary_email X, U use_email X ' |
|
151 'WHERE U login %(login)s', d, build_descr=False) |