def _users_query(self, q, limit=10):
     from simplifiedpermissionsadminplugin.simplifiedpermissions import SimplifiedPermissions
     if SimplifiedPermissions and self.env.is_enabled(SimplifiedPermissions):
         sp = SimplifiedPermissions(self.env)
         # Keep track of users that have already been found to prevent
         # yielding duplicates of users belonging to several groups
         yielded_sids = set()
         for group, data in sp.group_memberships().items():
             for member in data['members']:
                 if q in member.sid and member.sid not in yielded_sids:
                     # if the 'never logged in' text changes, then update
                     # plugins/open/autocompleteplugin/autocompleteplugin/htdocs/js/jquery.tracautocomplete.js
                     yield {'sid': member.sid,
                            'name': member.get('name', member.sid),
                            'email': member.get('email','')}
                     yielded_sids.add(member.sid)
     else:
         perm = PermissionSystem(self.env)
         users = [sid
                  for sid, permission in perm.get_all_permissions()
                  if sid not in set("anonymous", "authenticated", "admin")]
         for sid in sorted(set(users)):
             if q in sid:
                 session = DetachedSession(self.env, sid)
                 yield {'sid': sid,
                        'name': session.get('name',''),
                        'email': session.get('email','Never logged in')}
    def _project_users(self, all=False):
        """ Get project users """

        people = {}
        session_users = False
        from simplifiedpermissionsadminplugin.simplifiedpermissions import SimplifiedPermissions
        if SimplifiedPermissions and self.env.is_enabled(SimplifiedPermissions):
            shown_groups = AutoCompleteGroup(self.env).get_autocomplete_values('shown_groups')
            sp = SimplifiedPermissions(self.env)
            for group, data in sp.group_memberships().items():
                if all or group in shown_groups:
                    grouptitle = group.title().replace("_"," ")
                    if data['domains']:
                        group = "%s (Plus: %s)" % (group, ", ".join(data['domains']))
                        session_users = True
                    people[group] = {'name': grouptitle}
                    people[group]['members'] = [{'sid': member.sid,
                                                 'name': member.get('name', "%s (never logged in)" % member.sid),
                                                 'email': member.get('email', ''),
                                                 }
                                                for member in data['members']]
        else:
            session_users = True
        if session_users:
            known_users = self.env.get_known_users()
            people['Current Users'] = {'members': [{'sid': username,
                                                    'name': name,
                                                    'email': email,
                                                    }
                                                   for username, name, email in known_users]}

        return people
    def _get_user_data_(self, req, milestone, field, results, fields):
        """Get data grouped by users. Includes extra user info."""
        ats = AgileToolsSystem(self.env)
        sp = SimplifiedPermissions(self.env)

        tickets_json = defaultdict(lambda: defaultdict(dict))

        all_users = []
        user_data = {}
        use_avatar = self.config.get('avatar','mode').lower() != 'off'

        # TODO: allow the task board to respect user groups
        for group, data in sp.group_memberships().items():
            for member in data['members']:
                if member.sid not in user_data:
                    all_users.append(member.sid);
                    user_data[member.sid] = {
                        'name': member.get("name", member.sid),
                        'avatar': use_avatar and req.href.avatar(member.sid) or None
                    }

        def name_for_sid(sid):
            return user_data[sid]["name"] if sid in user_data else sid

        options = [""] + sorted(all_users, key=name_for_sid)

        for result in results:
            ticket = Ticket(self.env, result['id'])
            filtered_result = dict((k, v)
                                   for k, v in result.iteritems()
                                   if k in fields)
            filtered_result['position'] = ats.position(result['id'])
            filtered_result['_changetime'] = to_utimestamp(result['changetime'])
            # we use Trac's to_json() (through add_script_data), so
            # we'll replace any types which can't be json serialised
            for k, v in filtered_result.items():
                if isinstance(v, datetime): filtered_result[k] = pretty_age(v)
            group_field_val = ticket.get_value_or_default(field["name"]) or ""
            tickets_json[group_field_val][result["id"]] = filtered_result

        return (field["name"], tickets_json, options, user_data)