618 """check a user with the given login exists, if not create it with the |
618 """check a user with the given login exists, if not create it with the |
619 given password. This method is designed to be used for anonymous |
619 given password. This method is designed to be used for anonymous |
620 registration on public web site. |
620 registration on public web site. |
621 """ |
621 """ |
622 with self.internal_cnx() as cnx: |
622 with self.internal_cnx() as cnx: |
623 # for consistency, keep same error as unique check hook (although not required) |
623 with cnx.ensure_cnx_set: |
624 errmsg = cnx._('the value "%s" is already used, use another one') |
624 # for consistency, keep same error as unique check hook (although not required) |
625 if (cnx.execute('CWUser X WHERE X login %(login)s', {'login': login}, |
625 errmsg = cnx._('the value "%s" is already used, use another one') |
626 build_descr=False) |
626 if (cnx.execute('CWUser X WHERE X login %(login)s', {'login': login}, |
627 or cnx.execute('CWUser X WHERE X use_email C, C address %(login)s', |
627 build_descr=False) |
628 {'login': login}, build_descr=False)): |
628 or cnx.execute('CWUser X WHERE X use_email C, C address %(login)s', |
629 qname = role_name('login', 'subject') |
629 {'login': login}, build_descr=False)): |
630 raise ValidationError(None, {qname: errmsg % login}) |
630 qname = role_name('login', 'subject') |
631 # we have to create the user |
631 raise ValidationError(None, {qname: errmsg % login}) |
632 user = self.vreg['etypes'].etype_class('CWUser')(cnx) |
632 # we have to create the user |
633 if isinstance(password, unicode): |
633 user = self.vreg['etypes'].etype_class('CWUser')(cnx) |
634 # password should *always* be utf8 encoded |
634 if isinstance(password, unicode): |
635 password = password.encode('UTF8') |
635 # password should *always* be utf8 encoded |
636 kwargs['login'] = login |
636 password = password.encode('UTF8') |
637 kwargs['upassword'] = password |
637 kwargs['login'] = login |
638 self.glob_add_entity(cnx, EditedEntity(user, **kwargs)) |
638 kwargs['upassword'] = password |
639 cnx.execute('SET X in_group G WHERE X eid %(x)s, G name "users"', |
639 self.glob_add_entity(cnx, EditedEntity(user, **kwargs)) |
640 {'x': user.eid}) |
640 cnx.execute('SET X in_group G WHERE X eid %(x)s, G name "users"', |
641 if email or '@' in login: |
641 {'x': user.eid}) |
642 d = {'login': login, 'email': email or login} |
642 if email or '@' in login: |
643 if cnx.execute('EmailAddress X WHERE X address %(email)s', d, |
643 d = {'login': login, 'email': email or login} |
644 build_descr=False): |
644 if cnx.execute('EmailAddress X WHERE X address %(email)s', d, |
645 qname = role_name('address', 'subject') |
645 build_descr=False): |
646 raise ValidationError(None, {qname: errmsg % d['email']}) |
646 qname = role_name('address', 'subject') |
647 cnx.execute('INSERT EmailAddress X: X address %(email)s, ' |
647 raise ValidationError(None, {qname: errmsg % d['email']}) |
648 'U primary_email X, U use_email X ' |
648 cnx.execute('INSERT EmailAddress X: X address %(email)s, ' |
649 'WHERE U login %(login)s', d, build_descr=False) |
649 'U primary_email X, U use_email X ' |
650 cnx.commit() |
650 'WHERE U login %(login)s', d, build_descr=False) |
|
651 cnx.commit() |
651 return True |
652 return True |
652 |
653 |
653 def find_users(self, fetch_attrs, **query_attrs): |
654 def find_users(self, fetch_attrs, **query_attrs): |
654 """yield user attributes for cwusers matching the given query_attrs |
655 """yield user attributes for cwusers matching the given query_attrs |
655 (the result set cannot survive this method call) |
656 (the result set cannot survive this method call) |