entities/authobjs.py
author Rémi Cardona <remi.cardona@logilab.fr>
Tue, 08 Dec 2015 16:28:20 +0100
changeset 10969 b4de8b1cc135
parent 10612 84468b90e9c1
permissions -rw-r--r--
Use cubicweb._
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8544
3d049071957e massive copyright update to avoid clutering later patches
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8538
diff changeset
     1
# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5426
diff changeset
    18
"""entity classes user and group entities"""
583
d0c6f5efb837 fix rtags to have correct msgids for bookmarked_by relation
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    19
d0c6f5efb837 fix rtags to have correct msgids for bookmarked_by relation
sylvain.thenault@logilab.fr
parents: 0
diff changeset
    20
__docformat__ = "restructuredtext en"
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1343
diff changeset
    21
10612
84468b90e9c1 [py3k] basestring → six.string_types
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10141
diff changeset
    22
from six import string_types
84468b90e9c1 [py3k] basestring → six.string_types
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10141
diff changeset
    23
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
from logilab.common.decorators import cached
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
from cubicweb import Unauthorized
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
from cubicweb.entities import AnyEntity, fetch_config
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1343
diff changeset
    29
class CWGroup(AnyEntity):
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2784
diff changeset
    30
    __regid__ = 'CWGroup'
7827
9bbf83f68bcc [entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7797
diff changeset
    31
    fetch_attrs, cw_fetch_order = fetch_config(['name'])
9bbf83f68bcc [entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7797
diff changeset
    32
    cw_fetch_unrelated_order = cw_fetch_order
7782
40a49f4350a5 backout 7780:a1d5365fefc1
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7780
diff changeset
    33
7994
af3fb709c061 [management table] upgrade sources groups management table to new api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7845
diff changeset
    34
    def dc_long_title(self):
af3fb709c061 [management table] upgrade sources groups management table to new api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7845
diff changeset
    35
        name = self.name
af3fb709c061 [management table] upgrade sources groups management table to new api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7845
diff changeset
    36
        trname = self._cw._(name)
af3fb709c061 [management table] upgrade sources groups management table to new api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7845
diff changeset
    37
        if trname != name:
af3fb709c061 [management table] upgrade sources groups management table to new api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7845
diff changeset
    38
            return '%s (%s)' % (name, trname)
af3fb709c061 [management table] upgrade sources groups management table to new api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7845
diff changeset
    39
        return name
af3fb709c061 [management table] upgrade sources groups management table to new api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7845
diff changeset
    40
af3fb709c061 [management table] upgrade sources groups management table to new api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7845
diff changeset
    41
    @cached
af3fb709c061 [management table] upgrade sources groups management table to new api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7845
diff changeset
    42
    def num_users(self):
af3fb709c061 [management table] upgrade sources groups management table to new api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7845
diff changeset
    43
        """return the number of users in this group"""
af3fb709c061 [management table] upgrade sources groups management table to new api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7845
diff changeset
    44
        return self._cw.execute('Any COUNT(U) WHERE U in_group G, G eid %(g)s',
af3fb709c061 [management table] upgrade sources groups management table to new api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7845
diff changeset
    45
                                {'g': self.eid})[0][0]
af3fb709c061 [management table] upgrade sources groups management table to new api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7845
diff changeset
    46
4246
c95b8c7e5fb2 don't use matching_groups() for is_in_group implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    47
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1343
diff changeset
    48
class CWUser(AnyEntity):
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2784
diff changeset
    49
    __regid__ = 'CWUser'
7827
9bbf83f68bcc [entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7797
diff changeset
    50
    fetch_attrs, cw_fetch_order = fetch_config(['login', 'firstname', 'surname'])
9bbf83f68bcc [entity] upgrade fetch_[unrelated_]order to benefit from changes introduced in 3.14 (closes #1942758)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7797
diff changeset
    51
    cw_fetch_unrelated_order = cw_fetch_order
1553
3f91ef2397d0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    52
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
    # used by repository to check if  the user can log in or not
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
    AUTHENTICABLE_STATES = ('activated',)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
    # low level utilities #####################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
    def __init__(self, *args, **kwargs):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
        groups = kwargs.pop('groups', None)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
        properties = kwargs.pop('properties', None)
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1343
diff changeset
    60
        super(CWUser, self).__init__(*args, **kwargs)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
        if groups is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
            self._groups = groups
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
        if properties is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
            self._properties = properties
1553
3f91ef2397d0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    65
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
    @property
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
    def groups(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
            return self._groups
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
        except AttributeError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
            self._groups = set(g.name for g in self.in_group)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
            return self._groups
1553
3f91ef2397d0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    73
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
    @property
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
    def properties(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
            return self._properties
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
        except AttributeError:
10141
62e1f9749d3a [entities] Optimize CWUser.properties.
Christophe de Vienne <christophe@unlish.com>
parents: 9059
diff changeset
    79
            self._properties = dict(
62e1f9749d3a [entities] Optimize CWUser.properties.
Christophe de Vienne <christophe@unlish.com>
parents: 9059
diff changeset
    80
                self._cw.execute(
62e1f9749d3a [entities] Optimize CWUser.properties.
Christophe de Vienne <christophe@unlish.com>
parents: 9059
diff changeset
    81
                    'Any K, V WHERE P for_user U, U eid %(userid)s, '
62e1f9749d3a [entities] Optimize CWUser.properties.
Christophe de Vienne <christophe@unlish.com>
parents: 9059
diff changeset
    82
                    'P pkey K, P value V',
62e1f9749d3a [entities] Optimize CWUser.properties.
Christophe de Vienne <christophe@unlish.com>
parents: 9059
diff changeset
    83
                    {'userid': self.eid}))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
            return self._properties
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
8538
00597256de18 [request/session] refactor language handling: don't attempt to sync web/repo languages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8483
diff changeset
    86
    def prefered_language(self, language=None):
00597256de18 [request/session] refactor language handling: don't attempt to sync web/repo languages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8483
diff changeset
    87
        """return language used by this user, if explicitly defined (eg not
00597256de18 [request/session] refactor language handling: don't attempt to sync web/repo languages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8483
diff changeset
    88
        using http negociation)
00597256de18 [request/session] refactor language handling: don't attempt to sync web/repo languages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8483
diff changeset
    89
        """
00597256de18 [request/session] refactor language handling: don't attempt to sync web/repo languages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8483
diff changeset
    90
        language = language or self.property_value('ui.language')
00597256de18 [request/session] refactor language handling: don't attempt to sync web/repo languages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8483
diff changeset
    91
        vreg = self._cw.vreg
00597256de18 [request/session] refactor language handling: don't attempt to sync web/repo languages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8483
diff changeset
    92
        try:
00597256de18 [request/session] refactor language handling: don't attempt to sync web/repo languages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8483
diff changeset
    93
            vreg.config.translations[language]
00597256de18 [request/session] refactor language handling: don't attempt to sync web/repo languages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8483
diff changeset
    94
        except KeyError:
00597256de18 [request/session] refactor language handling: don't attempt to sync web/repo languages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8483
diff changeset
    95
            language = vreg.property_value('ui.language')
00597256de18 [request/session] refactor language handling: don't attempt to sync web/repo languages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8483
diff changeset
    96
            assert language in vreg.config.translations[language], language
00597256de18 [request/session] refactor language handling: don't attempt to sync web/repo languages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8483
diff changeset
    97
        return language
00597256de18 [request/session] refactor language handling: don't attempt to sync web/repo languages
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8483
diff changeset
    98
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
    def property_value(self, key):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
            # properties stored on the user aren't correctly typed
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
            # (e.g. all values are unicode string)
3380
3be33dc83d8b use ._cw.vreg instead of vreg on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3378
diff changeset
   103
            return self._cw.vreg.typed_value(key, self.properties[key])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
        except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
            pass
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
        except ValueError:
3380
3be33dc83d8b use ._cw.vreg instead of vreg on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3378
diff changeset
   107
            self.warning('incorrect value for eproperty %s of user %s',
3be33dc83d8b use ._cw.vreg instead of vreg on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3378
diff changeset
   108
                         key, self.login)
3be33dc83d8b use ._cw.vreg instead of vreg on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3378
diff changeset
   109
        return self._cw.vreg.property_value(key)
1553
3f91ef2397d0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   110
7110
73b3e0e095d3 [cwprops] add set_property method on CWUser instances, to ease setting of user specific properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   111
    def set_property(self, pkey, value):
73b3e0e095d3 [cwprops] add set_property method on CWUser instances, to ease setting of user specific properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   112
        value = unicode(value)
73b3e0e095d3 [cwprops] add set_property method on CWUser instances, to ease setting of user specific properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   113
        try:
73b3e0e095d3 [cwprops] add set_property method on CWUser instances, to ease setting of user specific properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   114
            prop = self._cw.execute(
73b3e0e095d3 [cwprops] add set_property method on CWUser instances, to ease setting of user specific properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   115
                'CWProperty X WHERE X pkey %(k)s, X for_user U, U eid %(u)s',
73b3e0e095d3 [cwprops] add set_property method on CWUser instances, to ease setting of user specific properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   116
                {'k': pkey, 'u': self.eid}).get_entity(0, 0)
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7782
diff changeset
   117
        except Exception:
7120
e8b6fc55df2d [cwuser, persistent properties] fix recent set_property implementation on CWUser: for_user is automatically handled for non-managers users (this is seriously debatable though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7110
diff changeset
   118
            kwargs = dict(pkey=unicode(pkey), value=value)
e8b6fc55df2d [cwuser, persistent properties] fix recent set_property implementation on CWUser: for_user is automatically handled for non-managers users (this is seriously debatable though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7110
diff changeset
   119
            if self.is_in_group('managers'):
e8b6fc55df2d [cwuser, persistent properties] fix recent set_property implementation on CWUser: for_user is automatically handled for non-managers users (this is seriously debatable though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7110
diff changeset
   120
                kwargs['for_user'] = self
e8b6fc55df2d [cwuser, persistent properties] fix recent set_property implementation on CWUser: for_user is automatically handled for non-managers users (this is seriously debatable though)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7110
diff changeset
   121
            self._cw.create_entity('CWProperty', **kwargs)
7110
73b3e0e095d3 [cwprops] add set_property method on CWUser instances, to ease setting of user specific properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   122
        else:
8483
4ba11607d84a [entity api] unify set_attributes / set_relations into a cw_set method. Closes #2423719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7994
diff changeset
   123
            prop.cw_set(value=value)
7110
73b3e0e095d3 [cwprops] add set_property method on CWUser instances, to ease setting of user specific properties
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   124
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
    def matching_groups(self, groups):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
        """return the number of the given group(s) in which the user is
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
        :type groups: str or iterable(str)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
        :param groups: a group name or an iterable on group names
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
        """
10612
84468b90e9c1 [py3k] basestring → six.string_types
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10141
diff changeset
   131
        if isinstance(groups, string_types):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
            groups = frozenset((groups,))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
        elif isinstance(groups, (tuple, list)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
            groups = frozenset(groups)
4246
c95b8c7e5fb2 don't use matching_groups() for is_in_group implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
   135
        return len(groups & self.groups) # XXX return the resulting set instead of its size
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   136
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   137
    def is_in_group(self, group):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
        """convience / shortcut method to test if the user belongs to `group`
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
        """
4402
360c532070f5 [entities] fix CWUser.is_in_group implementation
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 4246
diff changeset
   140
        return group in self.groups
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
590
bbf6d47a2e9b test if anonymous
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 0
diff changeset
   142
    def is_anonymous(self):
bbf6d47a2e9b test if anonymous
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 0
diff changeset
   143
        """ checks if user is an anonymous user"""
bbf6d47a2e9b test if anonymous
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 0
diff changeset
   144
        #FIXME on the web-side anonymous user is detected according
bbf6d47a2e9b test if anonymous
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 0
diff changeset
   145
        # to config['anonymous-user'], we don't have this info on
1553
3f91ef2397d0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   146
        # the server side.
590
bbf6d47a2e9b test if anonymous
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 0
diff changeset
   147
        return self.groups == frozenset(('guests', ))
bbf6d47a2e9b test if anonymous
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 0
diff changeset
   148
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   149
    def owns(self, eid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   150
        try:
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   151
            return self._cw.execute(
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4436
diff changeset
   152
                'Any X WHERE X eid %(x)s, X owned_by U, U eid %(u)s',
5174
78438ad513ca #759035: Automate addition of eid cachekey in RQL analysis
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
   153
                {'x': eid, 'u': self.eid})
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   154
        except Unauthorized:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   155
            return False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   156
    owns = cached(owns, keyarg=1)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   157
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   158
    # presentation utilities ##################################################
1553
3f91ef2397d0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   159
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   160
    def name(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   161
        """construct a name using firstname / surname or login if not defined"""
1553
3f91ef2397d0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
   162
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   163
        if self.firstname and self.surname:
3378
2f25f701301d use ._cw instead of req on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3377
diff changeset
   164
            return self._cw._('%(firstname)s %(surname)s') % {
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   165
                'firstname': self.firstname, 'surname' : self.surname}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   166
        if self.firstname:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   167
            return self.firstname
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   168
        return self.login
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   169
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   170
    def dc_title(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   171
        return self.login
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   172
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   173
    dc_long_title = name
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   174
9059
795ea498225f [cwuser] make CWUser callable, returning self for dbapi compatibility
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8544
diff changeset
   175
    def __call__(self, *args, **kwargs):
795ea498225f [cwuser] make CWUser callable, returning self for dbapi compatibility
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8544
diff changeset
   176
        """ugly hack for compatibility betweeb dbapi and repo api
795ea498225f [cwuser] make CWUser callable, returning self for dbapi compatibility
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8544
diff changeset
   177
795ea498225f [cwuser] make CWUser callable, returning self for dbapi compatibility
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8544
diff changeset
   178
        In the dbapi, Connection and Session have a ``user`` method to
795ea498225f [cwuser] make CWUser callable, returning self for dbapi compatibility
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8544
diff changeset
   179
        generated a user for a request In the repo api, Connection and Session
795ea498225f [cwuser] make CWUser callable, returning self for dbapi compatibility
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8544
diff changeset
   180
        have a user attribute inherited from SessionRequestBase prototype. This
795ea498225f [cwuser] make CWUser callable, returning self for dbapi compatibility
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8544
diff changeset
   181
        ugly hack allows to not break user of the user method.
795ea498225f [cwuser] make CWUser callable, returning self for dbapi compatibility
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8544
diff changeset
   182
795ea498225f [cwuser] make CWUser callable, returning self for dbapi compatibility
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8544
diff changeset
   183
        XXX Deprecate me ASAP"""
795ea498225f [cwuser] make CWUser callable, returning self for dbapi compatibility
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8544
diff changeset
   184
        return self
795ea498225f [cwuser] make CWUser callable, returning self for dbapi compatibility
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8544
diff changeset
   185
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   186
from logilab.common.deprecation import class_renamed
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1343
diff changeset
   187
EUser = class_renamed('EUser', CWUser)
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1343
diff changeset
   188
EGroup = class_renamed('EGroup', CWGroup)