예제 #1
0
    def authenticateCredentials(self, credentials):
        login = credentials.get('login')
        password = credentials.get('password')

        if login is None or password is None:
            return None

        # Adapt to IUserAuthentication to provide the actual authentication.
        # If no such adapter (or null-adapter) exists, fail.
        authentication = IUserAuthentication(self.context, None)
        if authentication is None:
            return None
            
        if authentication.verifyCredentials(credentials):
            userid = self.getUserId()
            return userid, login
예제 #2
0
파일: usermanager.py 프로젝트: a25kk/stv2
    def enumerateUsers( self
                      , id=None
                      , login=None
                      , exact_match=False
                      , sort_by=None
                      , max_results=None
                      , **kw
                      ):
        """ See IUserEnumerationPlugin.
        """
        user_info = []
        plugin_id = self.getId()
        view_name = createViewName('enumerateUsers', id or login)

        if isinstance( id, str ):
            id = [ id ]

        if isinstance( login, str ):
            login = [ login ]

        mbtool = getToolByName(self, TOOLNAME)
        query = {}

        # allow arbitrary indexes to be passed in to the catalog query
        query_index_map = IAnnotations(mbtool).get(QIM_ANNOT_KEY)
        if query_index_map is not None:
            for keyword in kw.keys():
                if keyword in query_index_map:
                    index_name = query_index_map[keyword]
                    search_term= kw[keyword]
                    if not exact_match:
                        index = mbtool.Indexes[index_name]
                        if type(index) == ZCTextIndex:
                            # split, glob, join
                            sep = search_term.strip().split()
                            sep = ["%s*" % val for val in sep]
                            search_term = ' '.join(sep)
                    query[index_name] = search_term

        # Look in the cache first...
        keywords = copy.deepcopy(kw)
        keywords.update( { 'id' : id
                         , 'login' : login
                         , 'exact_match' : exact_match
                         , 'sort_by' : sort_by
                         , 'max_results' : max_results
                         }
                       )
        cached_info = self.ZCacheable_get( view_name=view_name
                                         , keywords=keywords
                                         , default=None
                                         )
        if cached_info is not None:
            return tuple(cached_info)

        # Note: ZCTextIndex doesn't allow 'contains' searches AFAICT,
        #       so we use 'starts with'.
        if login:
            query['getUserName'] = exact_match and login or \
                                   ['%s*' % l for l in login]

        elif id:
            query['getUserId'] = exact_match and id or \
                                 ['%s*' % i for i in id]

        if sort_by is not None:
            if sort_by == 'login':
                query['sort_on'] = 'getUserName'
            if sort_by == 'id':
                query['sort_on'] = 'getUserId'

        query['object_implements'] = {'query': (IMembraneUserAuth.__identifier__,
                                                IUserAuthProvider.__identifier__,
                                                IUserAuthentication.__identifier__),
                                      'operator': 'and'}

        members = mbtool.unrestrictedSearchResults(**query)

        if max_results is not None:
            members = members[:max_results]

        for m in members:
            member = m._unrestrictedGetObject()
            authentication = IUserAuthentication(member)
            username = authentication.getUserName()
            authprovider = IUserAuthProvider(member)
            uid = authprovider.UID()
            userid = IMembraneUserAuth(member).getUserId()
            # XXX need to ask object for the edit URL, must adhere to an
            #     interface so we know we can stay within contract
            info = { 'id': userid
                     , 'login' : username
                     , 'pluginid': plugin_id
                     , 'editurl': '%s/base_edit' % member.absolute_url()
                     , 'uid': uid
                     }
            user_info.append(info)

        # Put the computed value into the cache
        self.ZCacheable_set(user_info, view_name=view_name, keywords=keywords)

        return tuple( user_info )