Skip to content
Permalink
Browse files

Merge pull request #1964 from privacyidea/545/filter_token_realm

Filter tokenrealm in UI
  • Loading branch information
NuvandaPV committed Dec 19, 2019
2 parents a8188d7 + c9e63b5 commit 39849a009ecfa17e036d8c9c4e64486d0d827187
@@ -182,13 +182,20 @@ def _create_token_query(tokentype=None, realm=None, assigned=None, user=None,
else:
log.warning("assigned value not in [True, False] {0!r}".format(assigned))

if realm is not None:
stripped_realm = None if realm is None else realm.strip("*")
if stripped_realm is not None:
# filter for the realm
sql_query = sql_query.filter(and_(func.lower(Realm.name) ==
realm.lower(),
TokenRealm.realm_id == Realm.id,
TokenRealm.token_id ==
Token.id)).distinct()
if "*" in realm:
sql_query = sql_query.filter(and_(func.lower(Realm.name).like(realm.replace("*", "%").lower()),
TokenRealm.realm_id == Realm.id,
TokenRealm.token_id ==
Token.id)).distinct()
else:
# exact matching
sql_query = sql_query.filter(and_(func.lower(Realm.name) == realm.lower(),
TokenRealm.realm_id == Realm.id,
TokenRealm.token_id ==
Token.id)).distinct()

if allowed_realms is not None:
sql_query = sql_query.filter(and_(func.lower(Realm.name).in_([r.lower() for r in allowed_realms]),
@@ -40,6 +40,7 @@ myApp.controller("tokenController", function (TokenFactory, ConfigFactory,
$scope.get = function (live_search) {
if ((!$rootScope.search_on_enter) || ($rootScope.search_on_enter && !live_search)) {
$scope.params.serial = "*" + ($scope.serialFilter || "") + "*";
$scope.params.tokenrealm = "*" + ($scope.tokenrealmFilter || "") + "*";
$scope.params.type = "*" + ($scope.typeFilter || "") + "*";
$scope.params.description = "*" + ($scope.descriptionFilter || "") + "*";
$scope.params.userid = "*" + ($scope.userIdFilter || "") + "*";
@@ -62,6 +62,12 @@
</th>
<th ng-show="loggedInUser.role == 'admin'" translate>user</th>
<th ng-show="loggedInUser.role == 'admin'" translate>realm</th>
<th class="pifilter" ng-show="loggedInUser.role == 'admin'" translate>Token realms
<pi-filter ng-model="tokenrealmFilter"
ng-change="get('livesearch')"
ng-keypress="($event.which==13)?get():return"
></pi-filter>
</th>
<th class="pifilter" ng-show="loggedInUser.role == 'admin' &&
user_details_in_tokenlist">
<button pi-sort-by="resolver"
@@ -131,6 +137,11 @@
<a ui-sref="config.realms.list">{{ token.user_realm }}</a>
</td>

<td ng-show="loggedInUser.role == 'admin'">
<a ui-sref="config.realms.list" ng-repeat="t_realm in token.realms">
{{ t_realm }}</a>&nbsp;
</td>

<td ng-show="loggedInUser.role == 'admin' &&
user_details_in_tokenlist">
{{ token.user_id }}
@@ -352,6 +352,24 @@ def test_03_list_tokens_in_one_realm(self):
self.assertTrue(len(tokenlist) == 2, res.data)
self.assertTrue(count == 2, count)

# list tokens, that look a bit like realm1
search_realm = self.realm1[:-1] + "*"
with self.app.test_request_context('/token/',
method='GET',
query_string=urlencode({
"tokenrealm": search_realm}),
headers={'Authorization': self.at}):
res = self.app.full_dispatch_request()
self.assertTrue(res.status_code == 200, res)
result = res.json.get("result")
detail = res.json.get("detail")
tokenlist = result.get("value").get("tokens")
count = result.get("value").get("count")
next = result.get("value").get("next")
prev = result.get("value").get("prev")
self.assertTrue(len(tokenlist) == 2, res.data)
self.assertTrue(count == 2, count)

def test_04_assign_unassign_token(self):
with self.app.test_request_context('/token/assign',
method='POST',

0 comments on commit 39849a0

Please sign in to comment.
You can’t perform that action at this time.