From 475e23da579b42dae4fec071b8bc8b51a3f437d6 Mon Sep 17 00:00:00 2001 From: Nabil Adouani Date: Tue, 30 Jun 2020 11:23:52 +0200 Subject: [PATCH] #1418 Add user details preloading --- .gitignore | 1 + frontend/app/scripts/app.js | 7 +- .../organisation/OrgCaseTemplateListCmp.js | 9 ++- .../app/scripts/directives/updatableUser.js | 9 ++- frontend/app/scripts/services/api/UserSrv.js | 79 +++++++++++-------- 5 files changed, 65 insertions(+), 40 deletions(-) diff --git a/.gitignore b/.gitignore index 9b6ea6c755..1353406954 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +dev logs bin conf/application.conf diff --git a/frontend/app/scripts/app.js b/frontend/app/scripts/app.js index 873adc9880..85471e4185 100644 --- a/frontend/app/scripts/app.js +++ b/frontend/app/scripts/app.js @@ -89,12 +89,15 @@ angular.module('thehive', [ templateUrl: 'views/app.html', controller: 'RootCtrl', resolve: { - currentUser: function($q, $state, AuthenticationSrv, NotificationSrv) { + currentUser: function($q, $state, UserSrv, AuthenticationSrv, NotificationSrv) { var deferred = $q.defer(); AuthenticationSrv.current() .then(function(userData) { - return deferred.resolve(userData); + // Prepare user cache + UserSrv.loadCache(userData.organisation); + + return deferred.resolve(userData); }) .catch( function(err) { NotificationSrv.error('App', err.data, err.status); diff --git a/frontend/app/scripts/components/organisation/OrgCaseTemplateListCmp.js b/frontend/app/scripts/components/organisation/OrgCaseTemplateListCmp.js index 8f1d8bd380..c624c9cd4f 100644 --- a/frontend/app/scripts/components/organisation/OrgCaseTemplateListCmp.js +++ b/frontend/app/scripts/components/organisation/OrgCaseTemplateListCmp.js @@ -179,8 +179,13 @@ return UserSrv.list( self.currentUser.organisation, { - filter: { locked: false }, - sort: [{'name': 'asc'}] + filter: { + _is: { + _field: 'locked', + _value: false + } + }, + sort: ['+name'] } ); }, diff --git a/frontend/app/scripts/directives/updatableUser.js b/frontend/app/scripts/directives/updatableUser.js index 29703ca9c6..e06f31edb9 100644 --- a/frontend/app/scripts/directives/updatableUser.js +++ b/frontend/app/scripts/directives/updatableUser.js @@ -19,8 +19,13 @@ if(value === true && !cached) { // TODO nadouani use {"_field": "locked": "_value": false} UserSrv.list(AuthenticationSrv.currentUser.organisation, { - filter: { locked: false }, - sort: [{'name': 'asc'}] + filter: { + _is: { + _field: 'locked', + _value: false + } + }, + sort: ['+name'] }) .then(function(users) { scope.userList = users; diff --git a/frontend/app/scripts/services/api/UserSrv.js b/frontend/app/scripts/services/api/UserSrv.js index 3ab87b79f4..714fc51d42 100644 --- a/frontend/app/scripts/services/api/UserSrv.js +++ b/frontend/app/scripts/services/api/UserSrv.js @@ -184,57 +184,45 @@ } ]; + var queryConfig = {}; + // Apply filter is defined if (options && options.filter) { - // operations.push({ - // '_name': 'filter', - // '_is': options.filter - // }); - - operations.push( - _.extend({'_name': 'filter'}, {'_is': options.filter}) - ); + queryConfig.filter = options.filter; } // Apply sort is defined if (options && options.sort) { - // operations.push({ - // '_name': 'sort', - // '_fields': options.sort - // }); - - operations.push( - _.extend({'_name': 'sort'}, {'_fields': options.sort}) - ); + queryConfig.sort = options.sort; } - return QuerySrv.query('v1', operations) - .then(function(response) { - return $q.resolve(response.data); - }); + return QuerySrv.call('v1', operations, queryConfig); }; this.autoComplete = function(organisation, query) { + // TODO nadouani filter on server side return this.list(organisation, { + filter: { _is: { locked: false } - }) - .then(function(data) { - return _.map(data, function(user) { - return { - label: user.name, - text: user.login - }; - }); - }) - .then(function(users) { - return _.filter(users, function(user) { - var regex = new RegExp(query, 'gi'); + } + }) + .then(function(data) { + return _.map(data, function(user) { + return { + label: user.name, + text: user.login + }; + }); + }) + .then(function(users) { + return _.filter(users, function(user) { + var regex = new RegExp(query, 'gi'); - return regex.test(user.label); - }); + return regex.test(user.label); }); + }); }; this.getCache = function(userId) { @@ -268,6 +256,29 @@ self.userCache[userId] = userData; }; + + /** + * Cache the details of all the visible users + */ + this.loadCache = function() { + var defer = $q.defer(); + + QuerySrv.call('v1', [ + {'_name': 'listOrganisation'}, + {'_name': 'users'}, + ]) + .then(function(users) { + _.each(users, function(u) { + self.updateCache(u.login, u); + }); + defer.resolve(); + }).catch(function(err){ + defer.reject(err); + }); + + return defer.promise; + }; + this.openModal = function(user, organisation) { var modalInstance = $uibModal.open({ templateUrl: 'views/partials/admin/organisation/user.modal.html',