14 # details. |
14 # details. |
15 # |
15 # |
16 # You should have received a copy of the GNU Lesser General Public License along |
16 # You should have received a copy of the GNU Lesser General Public License along |
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
18 """entity classes user and group entities""" |
18 """entity classes user and group entities""" |
19 |
|
20 from six import string_types, text_type |
|
21 |
19 |
22 from logilab.common.decorators import cached |
20 from logilab.common.decorators import cached |
23 |
21 |
24 from cubicweb import Unauthorized |
22 from cubicweb import Unauthorized |
25 from cubicweb.entities import AnyEntity, fetch_config |
23 from cubicweb.entities import AnyEntity, fetch_config |
108 self.warning('incorrect value for eproperty %s of user %s', |
106 self.warning('incorrect value for eproperty %s of user %s', |
109 key, self.login) |
107 key, self.login) |
110 return self._cw.vreg.property_value(key) |
108 return self._cw.vreg.property_value(key) |
111 |
109 |
112 def set_property(self, pkey, value): |
110 def set_property(self, pkey, value): |
113 value = text_type(value) |
|
114 try: |
111 try: |
115 prop = self._cw.execute( |
112 prop = self._cw.execute( |
116 'CWProperty X WHERE X pkey %(k)s, X for_user U, U eid %(u)s', |
113 'CWProperty X WHERE X pkey %(k)s, X for_user U, U eid %(u)s', |
117 {'k': pkey, 'u': self.eid}).get_entity(0, 0) |
114 {'k': pkey, 'u': self.eid}).get_entity(0, 0) |
118 except Exception: |
115 except Exception: |
119 kwargs = dict(pkey=text_type(pkey), value=value) |
116 kwargs = dict(pkey=pkey, value=value) |
120 if self.is_in_group('managers'): |
117 if self.is_in_group('managers'): |
121 kwargs['for_user'] = self |
118 kwargs['for_user'] = self |
122 self._cw.create_entity('CWProperty', **kwargs) |
119 self._cw.create_entity('CWProperty', **kwargs) |
123 else: |
120 else: |
124 prop.cw_set(value=value) |
121 prop.cw_set(value=value) |
127 """return the number of the given group(s) in which the user is |
124 """return the number of the given group(s) in which the user is |
128 |
125 |
129 :type groups: str or iterable(str) |
126 :type groups: str or iterable(str) |
130 :param groups: a group name or an iterable on group names |
127 :param groups: a group name or an iterable on group names |
131 """ |
128 """ |
132 if isinstance(groups, string_types): |
129 if isinstance(groups, str): |
133 groups = frozenset((groups,)) |
130 groups = frozenset((groups,)) |
134 elif isinstance(groups, (tuple, list)): |
131 elif isinstance(groups, (tuple, list)): |
135 groups = frozenset(groups) |
132 groups = frozenset(groups) |
136 return len(groups & self.groups) # XXX return the resulting set instead of its size |
133 return len(groups & self.groups) # XXX return the resulting set instead of its size |
137 |
134 |