39 |
39 |
40 from logilab.common.decorators import cached, clear_cache |
40 from logilab.common.decorators import cached, clear_cache |
41 from logilab.common.deprecation import deprecated |
41 from logilab.common.deprecation import deprecated |
42 |
42 |
43 from yams import BadSchemaDefinition |
43 from yams import BadSchemaDefinition |
44 from yams.schema import role_name |
|
45 from rql import RQLSyntaxError |
44 from rql import RQLSyntaxError |
46 from rql.utils import rqlvar_maker |
45 from rql.utils import rqlvar_maker |
47 |
46 |
48 from cubicweb import (CW_MIGRATION_MAP, QueryError, |
47 from cubicweb import (CW_MIGRATION_MAP, QueryError, |
49 UnknownEid, AuthenticationError, ExecutionError, |
48 UnknownEid, AuthenticationError, ExecutionError, |
609 # don't use cnx.execute, we don't want rset.req set |
608 # don't use cnx.execute, we don't want rset.req set |
610 return self.querier.execute(cnx, 'Any K,V WHERE P is CWProperty,' |
609 return self.querier.execute(cnx, 'Any K,V WHERE P is CWProperty,' |
611 'P pkey K, P value V, NOT P for_user U', |
610 'P pkey K, P value V, NOT P for_user U', |
612 build_descr=False) |
611 build_descr=False) |
613 |
612 |
614 # XXX protect this method: anonymous should be allowed and registration |
613 @deprecated("[3.19] Use session.call_service('register_user') instead'") |
615 # plugged |
|
616 def register_user(self, login, password, email=None, **kwargs): |
614 def register_user(self, login, password, email=None, **kwargs): |
617 """check a user with the given login exists, if not create it with the |
615 """check a user with the given login exists, if not create it with the |
618 given password. This method is designed to be used for anonymous |
616 given password. This method is designed to be used for anonymous |
619 registration on public web site. |
617 registration on public web site. |
620 """ |
618 """ |
621 with self.internal_cnx() as cnx: |
619 with self.internal_cnx() as cnx: |
622 # for consistency, keep same error as unique check hook (although not required) |
620 cnx.call_service('register_user', login=login, password=password, |
623 errmsg = cnx._('the value "%s" is already used, use another one') |
621 email=email, **kwargs) |
624 if (cnx.execute('CWUser X WHERE X login %(login)s', {'login': login}, |
|
625 build_descr=False) |
|
626 or cnx.execute('CWUser X WHERE X use_email C, C address %(login)s', |
|
627 {'login': login}, build_descr=False)): |
|
628 qname = role_name('login', 'subject') |
|
629 raise ValidationError(None, {qname: errmsg % login}) |
|
630 # we have to create the user |
|
631 user = self.vreg['etypes'].etype_class('CWUser')(cnx) |
|
632 if isinstance(password, unicode): |
|
633 # password should *always* be utf8 encoded |
|
634 password = password.encode('UTF8') |
|
635 kwargs['login'] = login |
|
636 kwargs['upassword'] = password |
|
637 self.glob_add_entity(cnx, EditedEntity(user, **kwargs)) |
|
638 cnx.execute('SET X in_group G WHERE X eid %(x)s, G name "users"', |
|
639 {'x': user.eid}) |
|
640 if email or '@' in login: |
|
641 d = {'login': login, 'email': email or login} |
|
642 if cnx.execute('EmailAddress X WHERE X address %(email)s', d, |
|
643 build_descr=False): |
|
644 qname = role_name('address', 'subject') |
|
645 raise ValidationError(None, {qname: errmsg % d['email']}) |
|
646 cnx.execute('INSERT EmailAddress X: X address %(email)s, ' |
|
647 'U primary_email X, U use_email X ' |
|
648 'WHERE U login %(login)s', d, build_descr=False) |
|
649 cnx.commit() |
622 cnx.commit() |
650 return True |
|
651 |
623 |
652 def find_users(self, fetch_attrs, **query_attrs): |
624 def find_users(self, fetch_attrs, **query_attrs): |
653 """yield user attributes for cwusers matching the given query_attrs |
625 """yield user attributes for cwusers matching the given query_attrs |
654 (the result set cannot survive this method call) |
626 (the result set cannot survive this method call) |
655 |
627 |