web/views/cwuser.py
changeset 7800 ea496a3ed703
parent 7777 2764987eb17a
child 7802 d0bf71aff4a1
equal deleted inserted replaced
7798:8930f7a284dd 7800:ea496a3ed703
    66          xmlns:foaf="http://xmlns.com/foaf/0.1/"> '''% self._cw.encoding)
    66          xmlns:foaf="http://xmlns.com/foaf/0.1/"> '''% self._cw.encoding)
    67         for i in xrange(self.cw_rset.rowcount):
    67         for i in xrange(self.cw_rset.rowcount):
    68             self.cell_call(i, 0)
    68             self.cell_call(i, 0)
    69         self.w(u'</rdf:RDF>\n')
    69         self.w(u'</rdf:RDF>\n')
    70 
    70 
    71     def cell_call(self, row, col):
    71     def entity_call(self, entity, **kwargs):
    72         entity = self.cw_rset.complete_entity(row, col)
    72         entity.complete()
    73         # account
    73         # account
    74         self.w(u'<foaf:OnlineAccount rdf:about="%s">\n' % entity.absolute_url())
    74         self.w(u'<foaf:OnlineAccount rdf:about="%s">\n' % entity.absolute_url())
    75         self.w(u'  <foaf:accountName>%s</foaf:accountName>\n' % entity.login)
    75         self.w(u'  <foaf:accountName>%s</foaf:accountName>\n' % entity.login)
    76         self.w(u'</foaf:OnlineAccount>\n')
    76         self.w(u'</foaf:OnlineAccount>\n')
    77         # person
    77         # person
   124 
   124 
   125 class CWGroupPermTab(EntityView):
   125 class CWGroupPermTab(EntityView):
   126     __regid__ = 'cwgroup-permissions'
   126     __regid__ = 'cwgroup-permissions'
   127     __select__ = is_instance('CWGroup')
   127     __select__ = is_instance('CWGroup')
   128 
   128 
   129     def cell_call(self, row, col):
   129     def entity_call(self, entity):
   130         self._cw.add_css(('cubicweb.schema.css','cubicweb.acl.css'))
   130         self._cw.add_css(('cubicweb.schema.css','cubicweb.acl.css'))
   131         access_types = ('read', 'delete', 'add', 'update')
   131         access_types = ('read', 'delete', 'add', 'update')
   132         w = self.w
   132         w = self.w
   133         entity = self.cw_rset.get_entity(row, col)
       
   134         objtype_access = {'CWEType': ('read', 'delete', 'add', 'update'),
   133         objtype_access = {'CWEType': ('read', 'delete', 'add', 'update'),
   135                           'CWRelation': ('add', 'delete')}
   134                           'CWRelation': ('add', 'delete')}
   136         rql_cwetype = 'DISTINCT Any X WHERE X %s_permission CWG, X is CWEType, ' \
   135         rql_cwetype = 'DISTINCT Any X WHERE X %s_permission CWG, X is CWEType, ' \
   137                       'CWG eid %%(e)s'
   136                       'CWG eid %%(e)s'
   138         rql_cwrelation = 'DISTINCT Any RT WHERE X %s_permission CWG, X is CWRelation, ' \
   137         rql_cwrelation = 'DISTINCT Any RT WHERE X %s_permission CWG, X is CWRelation, ' \
   146             rset = self._cw.execute(rql % access_type, {'e': entity.eid})
   145             rset = self._cw.execute(rql % access_type, {'e': entity.eid})
   147             if rset:
   146             if rset:
   148                 self.w(u'<div>%s:</div>' % self._cw.__(access_type + '_permission'))
   147                 self.w(u'<div>%s:</div>' % self._cw.__(access_type + '_permission'))
   149                 self.w(u'<div>%s</div><br/>' % self._cw.view('csv', rset, 'null'))
   148                 self.w(u'<div>%s</div><br/>' % self._cw.view('csv', rset, 'null'))
   150 
   149 
       
   150 
   151 class CWGroupInContextView(EntityView):
   151 class CWGroupInContextView(EntityView):
   152     __regid__ = 'incontext'
   152     __regid__ = 'incontext'
   153     __select__ = is_instance('CWGroup')
   153     __select__ = is_instance('CWGroup')
   154 
   154 
   155     def cell_call(self, row, col):
   155     def entity_call(self, entity, **kwargs):
   156         entity = self.cw_rset.complete_entity(row, col)
   156         entity.complete()
   157         self.w(u'<a href="%s" class="%s">%s</a>' % (
   157         self.w(u'<a href="%s" class="%s">%s</a>' % (
   158             entity.absolute_url(), entity.name, entity.printable_value('name')))
   158             entity.absolute_url(), entity.name, entity.printable_value('name')))
   159 
   159 
   160 
   160 
   161 # user / groups management views ###############################################
   161 # user / groups management views ###############################################
   164     __regid__ = 'cwuser' # see rewrite rule /cwuser
   164     __regid__ = 'cwuser' # see rewrite rule /cwuser
   165     title = _('users and groups')
   165     title = _('users and groups')
   166     category = 'manage'
   166     category = 'manage'
   167 
   167 
   168 
   168 
       
   169 class UsersAndGroupsManagementView(tabs.TabsMixin, StartupView):
       
   170     __regid__ = 'cw.users-and-groups-management'
       
   171     __select__ = StartupView.__select__ & match_user_groups('managers')
       
   172     title = _('Users and groups management')
       
   173     tabs = [_('cw.users-management'), _('cw.groups-management'),]
       
   174     default_tab = 'cw.users-management'
       
   175 
       
   176     def call(self, **kwargs):
       
   177         """The default view representing the instance's management"""
       
   178         self.w(u'<h1>%s</h1>' % self._cw._(self.title))
       
   179         self.render_tabs(self.tabs, self.default_tab)
       
   180 
       
   181 
   169 class CWUserManagementView(StartupView):
   182 class CWUserManagementView(StartupView):
   170     __regid__ = 'cw.user-management'
   183     __regid__ = 'cw.users-management'
       
   184     __select__ = StartupView.__select__ & match_user_groups('managers')
       
   185     cache_max_age = 0 # disable caching
   171     # XXX one could wish to display for instance only user's firstname/surname
   186     # XXX one could wish to display for instance only user's firstname/surname
   172     # for non managers but filtering out NULL cause crash with an ldapuser
   187     # for non managers but filtering out NULL cause crash with an ldapuser
   173     # source.
   188     # source.
   174     __select__ = StartupView.__select__ & match_user_groups('managers')
   189     rql = ('Any U,US,F,S,U,UAA,UDS, L,UAA,USN,UDSN ORDERBY L WHERE U is CWUser, '
   175     rql = ('Any U,USN,F,S,U,UAA,UDS, L,UAA,UDSN ORDERBY L WHERE U is CWUser, '
       
   176            'U login L, U firstname F, U surname S, '
   190            'U login L, U firstname F, U surname S, '
   177            'U in_state US, US name USN, '
   191            'U in_state US, US name USN, '
   178            'U primary_email UA?, UA address UAA, '
   192            'U primary_email UA?, UA address UAA, '
   179            'U cw_source UDS, US name UDSN')
   193            'U cw_source UDS, US name UDSN')
   180     title = _('users and groups management')
   194 
       
   195     def call(self, **kwargs):
       
   196         add_button(self, 'CWUser')
       
   197         self.w(u'<div class="clear"></div>')
       
   198         self.wview('cw.users-table', self._cw.execute(self.rql))
       
   199 
       
   200 
       
   201 class CWGroupsManagementView(StartupView):
       
   202     __regid__ = 'cw.groups-management'
       
   203     __select__ = StartupView.__select__ & match_user_groups('managers')
   181     cache_max_age = 0 # disable caching
   204     cache_max_age = 0 # disable caching
   182 
   205     rql = ('Any G,COUNT(U), GN GROUPBY G,GN ORDERBY GN '
   183     def call(self, **kwargs):
   206            'WHERE G is CWGroup, U? in_group G, G name GN, NOT G name "owners"')
   184         self.w('<h1>%s</h1>' % self._cw._(self.title))
   207     headers = [None, None]
   185         for etype in ('CWUser', 'CWGroup'):
   208     cellvids = {}
   186             eschema = self._cw.vreg.schema.eschema(etype)
   209 
   187             if eschema.has_perm(self._cw, 'add'):
   210     def call(self, **kwargs):
   188                 self.w(u'<a href="%s" class="addButton right">%s</a>' % (
   211         add_button(self, 'CWGroup')
   189                     self._cw.build_url('add/%s' % eschema),
       
   190                     self._cw.__('New %s' % etype).capitalize()))
       
   191         self.w(u'<div class="clear"></div>')
   212         self.w(u'<div class="clear"></div>')
   192         self.wview('cw.user-table', self._cw.execute(self.rql))
   213         self.wview('editable-table', self._cw.execute(self.rql),
   193 
   214                    headers=self.headers, cellvids=self.cellvids)
   194 
   215 
   195 class CWUserTable(tableview.EditableTableView):
   216 
   196     __regid__ = 'cw.user-table'
   217 def add_button(self, etype):
       
   218     eschema = self._cw.vreg.schema.eschema(etype)
       
   219     if eschema.has_perm(self._cw, 'add'):
       
   220         self.w(u'<a href="%s" class="addButton right">%s</a>' % (
       
   221                 self._cw.build_url('add/%s' % eschema),
       
   222                 self._cw.__('New %s' % etype).capitalize()))
       
   223 
       
   224 
       
   225 class CWUsersTable(tableview.EditableTableView):
       
   226     __regid__ = 'cw.users-table'
   197     __select__ = is_instance('CWUser')
   227     __select__ = is_instance('CWUser')
   198 
   228 
   199     def call(self, **kwargs):
   229     def call(self, **kwargs):
   200         headers = (display_name(self._cw, 'CWUser', 'plural'),
   230         headers = (display_name(self._cw, 'CWUser', 'plural'),
   201                    display_name(self._cw, 'in_state'),
   231                    display_name(self._cw, 'in_state'),
   202                    self._cw._('firstname'), self._cw._('surname'),
   232                    self._cw._('firstname'), self._cw._('surname'),
   203                    display_name(self._cw, 'CWGroup', 'plural'),
   233                    display_name(self._cw, 'CWGroup', 'plural'),
   204                    display_name(self._cw, 'primary_email'),
   234                    display_name(self._cw, 'primary_email'),
   205                    display_name(self._cw, 'CWSource'))
   235                    display_name(self._cw, 'CWSource'))
   206         super(CWUserTable, self).call(
   236         super(CWUsersTable, self).call(
   207             paginate=True, displayfilter=True,
   237             paginate=True, displayfilter=True,
   208             cellvids={0: 'cw.user.login',
   238             cellvids={0: 'cw.user.login',
   209                       4: 'cw.user-table.group-cell'},
   239                       4: 'cw.users-table.group-cell'},
   210             headers=headers, **kwargs)
   240             headers=headers, **kwargs)
   211 
   241 
   212 
   242 
   213 class CWUserGroupCell(EntityView):
   243 class CWUserGroupCell(EntityView):
   214     __regid__ = 'cw.user-table.group-cell'
   244     __regid__ = 'cw.users-table.group-cell'
   215     __select__ = is_instance('CWUser')
   245     __select__ = is_instance('CWUser')
   216 
   246 
   217     def cell_call(self, row, col, **kwargs):
   247     def entity_call(self, entity, **kwargs):
   218         entity = self.cw_rset.get_entity(row, col)
       
   219         self.w(entity.view('reledit', rtype='in_group', role='subject'))
   248         self.w(entity.view('reledit', rtype='in_group', role='subject'))
       
   249 
   220 
   250 
   221 class CWUserLoginCell(EntityView):
   251 class CWUserLoginCell(EntityView):
   222     __regid__ = 'cw.user.login'
   252     __regid__ = 'cw.user.login'
   223     __select__ = is_instance('CWUser')
   253     __select__ = is_instance('CWUser')
   224 
   254 
   225     def cell_call(self, row, col, **kwargs):
   255     def entity_call(self, entity, **kwargs):
   226         entity = self.cw_rset.get_entity(row, col)
       
   227         self.w(tags.a(entity.login, href=entity.absolute_url()))
   256         self.w(tags.a(entity.login, href=entity.absolute_url()))