This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository mum. See http://git.chorem.org/mum.git commit 017493c6b978fdcacf8d595bc4aa983f0dee34e1 Author: Alexis Guilbaud <guilbaud@codelutin.com> Date: Thu Mar 19 15:40:29 2015 +0100 notifications page : possibilité d'ajouter un utilisateur à la liste des inscrits d'un groupe et d'un hôte --- app/modules/storage_modules/shelve_db.py | 153 +++++++++++++++++++++++++---- static/js/controllers/notificationsCtrl.js | 74 +++++++++++++- views/notifications.html | 30 +++++- 3 files changed, 233 insertions(+), 24 deletions(-) diff --git a/app/modules/storage_modules/shelve_db.py b/app/modules/storage_modules/shelve_db.py index 19e5776..69c6c65 100644 --- a/app/modules/storage_modules/shelve_db.py +++ b/app/modules/storage_modules/shelve_db.py @@ -735,7 +735,7 @@ class shelve_db: if group not in self.db['groups']: self.db['groups'][group] = {} self.db['groups'][group]['hosts'] = [] - self.db['groups'][group]['subscribers'] = [] + self.db['groups'][group]['subscribers'] = {} for host in host_list: if group not in self.db['hosts'][host]['conf']['groups']: self.db['groups'][group]['hosts'].append(host) @@ -765,6 +765,73 @@ class shelve_db: finally: self.close_db() + def subscribe_to_group(self, args): + """ + Add a list of users to the group notifications structure. + :param args: a dictionary containing : + { 'users': list<string>, 'group': val } + """ + users = args['users'] + group = args['group'] + self.open_db() + try: + for user in users: + self.db['groups'][group]['subscribers'][user] = {} + finally: + self.close_db() + + def update_subscription_to_group(self, args): + """ + Updates the subscription to a following user from a host. + :param args: + { 'group': val, + 'username': val, + 'subscription: [ + { notif_mod: priority }, + ... + ] + } + :return: + """ + group = args['group'] + username = args['username'] + self.open_db() + try: + self.db['groups'][group]['subscribers'][username] = args['subscription'] + finally: + self.close_db() + + def unsubscribe_to_group(self, args): + """ + Unsubscribes an user for a host notifications. + :param args: a dictionary containing : + { 'username': val, 'group': val } + """ + username = args['username'] + group = args['group'] + self.open_db() + try: + del self.db['groups'][group]['subscribers'][username] + finally: + self.close_db() + + def get_group_subscribers(self, args): + """ + + :param args: A dictionary containing : + { 'group': val } + :return: a list containing : + [{ 'username': val, 'priority': val }, ... ] + """ + group = args['group'] + res = [] + self.open_db() + try: + res = self.db['groups'][group]['subscribers'] + finally: + self.close_db() + return json.dumps(res) + def create_user(self, args): """ Create a basic empty structure on the database for a new user. @@ -799,7 +866,8 @@ class shelve_db: def remove_user(self, args): """ - Removes a user from the database. If the user is registered to a host or a group, it is also deleted from these lists + Removes a user from the database. If the user is registered to a host or a group, + it is also deleted from these lists :param args: a dictionary containing : { 'username': val } """ @@ -810,29 +878,78 @@ class shelve_db: del self.db['users'][username] # unregistering user from hosts for host in self.db['hosts']: - for subscriber in self.db['hosts'][host]['conf']['subscribers']: - if username in subscriber['username']: - self.db['hosts'][host]['conf']['subscribers'].remove(subscriber) + if username in self.db['hosts'][host]['subscribers']: + del self.db['hosts'][host]['conf']['subscribers'][username] # unregistering user form groups for group in self.db['groups']: - for subscriber in self.db['groups'][group]['subscribers']: - if username in subscriber['username']: - self.db['groups'][group]['subscribers'].remove(subscriber) + if username in self.db['groups'][group]['subscribers']: + del self.db['groups'][group]['subscribers'][username] finally: self.close_db() - def get_host_os(self, addr_host): + def subscribe_to_host(self, args): """ - Get the operating system corresponding to a host - :param addr_host: the IP adress of host to retreive os - :return: the os type corresponding to what have been detected + Add a list of users to the host notifications structure. + :param args: a dictionary containing : + { 'users': list<string>, 'addr_host': val } """ + users = args['users'] + addr_host = args['addr_host'] self.open_db() try: - detected = json.loads(self.db["hosts"][addr_host]["detected"]["nmap"]) - os = detected['os'].lower() - if os == "Unknown": - raise Exception('Os not detected') - return os + for user in users: + self.db['hosts'][addr_host]['conf']['subscribers'][user] = {} finally: - self.close_db() \ No newline at end of file + self.close_db() + + def update_subscription_to_host(self, args): + """ + Updates the subscription to a following user from a host. + :param args: + { 'addr_host': val, + 'username': val, + 'subscription: [ + { notif_mod: priority }, + ... + ] + } + :return: + """ + addr_host = args['addr_host'] + username = args['username'] + self.open_db() + try: + self.db['hosts'][addr_host]['conf']['subscribers'][username] = args['subscription'] + finally: + self.close_db() + + def unsubscribe_to_host(self, args): + """ + Unsubscribes an user for host notifications. + :param args: a dictionary containing : + { 'username': val, 'addr_host': val } + """ + username = args['username'] + addr_host = args['addr_host'] + self.open_db() + try: + del self.db['hosts'][addr_host]['conf']['subscribers'][username] + finally: + self.close_db() + + def get_host_subscribers(self, args): + """ + Get informations about the subscribers to a host. + :param args: A dictionary containing : + { 'addr_host': val } + :return: a dictionary containing : + { username: [{ 'notif_mod': val, 'priority': val }, ... ]} + """ + addr_host = args['addr_host'] + res = {} + self.open_db() + try: + res = self.db['hosts'][addr_host]['conf']['subscribers'] + finally: + self.close_db() + return json.dumps(res) \ No newline at end of file diff --git a/static/js/controllers/notificationsCtrl.js b/static/js/controllers/notificationsCtrl.js index 95e2cf1..fc4af3e 100644 --- a/static/js/controllers/notificationsCtrl.js +++ b/static/js/controllers/notificationsCtrl.js @@ -1,4 +1,4 @@ -mumApp.controller('notificationsCtrl', function($scope, $rootScope, DataHosts) { +mumApp.controller('notificationsCtrl', function($scope, $rootScope, $modal, DataHosts) { $scope.items = DataHosts.Items; /* [ { "addr":"192.168.74.1", @@ -17,6 +17,8 @@ mumApp.controller('notificationsCtrl', function($scope, $rootScope, DataHosts) { ] */ + $scope.subscriber_data = {}; + $scope.allGroups = function(){ var res = [] for(var i = 0; i<$scope.items.length; i++){ @@ -31,4 +33,74 @@ mumApp.controller('notificationsCtrl', function($scope, $rootScope, DataHosts) { $scope.selected_host = ""; $scope.option_selected = ""; + + $scope.get_group_subscribers = function(){ + if($scope.selected_grp != ""){ + var args = {'group': $scope.selected_grp}; + $rootScope.$broadcast("sendViaWs", JSON.stringify({"CALL_FUNC_DB": {'func': 'get_group_subscribers', 'args': args}})); + } + } + + $scope.get_host_subscribers = function(){ + if($scope.selected_host != ""){ + var args = {'addr_host': $scope.selected_host['addr']}; + $rootScope.$broadcast("sendViaWs", JSON.stringify({"CALL_FUNC_DB": {'func': 'get_host_subscribers', 'args': args}})); + } + } + + $scope.$on("resCall", function (event, args) { + $scope.$apply(function(){ + $scope.subscriber_data = args; + }); + }); + + $scope.open_modal_add_subscriber = function (mod_name) { + var modalInstance = $modal.open({ + templateUrl: 'modal_add_subscriber_label.html', + controller: 'ModalAddSubscriberInstanceCtrl', + resolve: { + add_subscriber_args: function(){ + return {"grp" : $scope.selected_grp, + "host" : $scope.selected_host}; + } + } + }); + }; +}); + +mumApp.controller('ModalAddSubscriberInstanceCtrl', function ($scope, $rootScope, $modalInstance, $route, add_subscriber_args) { + $scope.add_subscriber_args = add_subscriber_args; /* {'grp': val, 'host': val */ + + $scope.users = {}; + + $scope.selected_users = []; + + $rootScope.$broadcast("sendViaWs", JSON.stringify({"CALL_FUNC_DB": {'func': 'get_users', 'args': null}})); + + // receiving the user list + $scope.$on("resCall", function (event, args) { + $scope.$apply(function(){ + $scope.users = args; + }); + }); + + $scope.ok = function () { + var args = {}; + args['users'] = $scope.selected_users; + if($scope.add_subscriber_args['grp'] != ""){ + args['group'] = $scope.add_subscriber_args['grp']; + $rootScope.$broadcast("sendViaWs", JSON.stringify({"CALL_FUNC_DB": {'func': 'subscribe_to_group', 'args': args}})); + } + else{ + args['addr_host'] = $scope.add_subscriber_args['host']['addr']; + $rootScope.$broadcast("sendViaWs", JSON.stringify({"CALL_FUNC_DB": {'func': 'subscribe_to_host', 'args': args}})); + } + $modalInstance.close(); + + $route.reload(); + }; + + $scope.cancel = function () { + $modalInstance.close(); + }; }); \ No newline at end of file diff --git a/views/notifications.html b/views/notifications.html index 17bd3ac..f4afc3a 100644 --- a/views/notifications.html +++ b/views/notifications.html @@ -2,6 +2,7 @@ <div class="col-md-offset-2 main"> <h1 class="page-header">Who to notify?</h1> <!--<h2 class="sub-header">They will be applied on each new host you will add.</h2>--> + {{subscriber_data}} <form> <div class="row"> <div class="col-lg-6"> @@ -14,7 +15,8 @@ <select class="form-control input-sm" id="grp_choice" ng-model="selected_grp" ng-options="item for item in allGroups() | unique:'group'" - ng-disabled="option_selected!='grp'"></select> + ng-disabled="option_selected!='grp'" + ng-change="get_group_subscribers()"></select> </div> </div> <div class="col-lg-6"> @@ -27,7 +29,8 @@ <select class="form-control input-sm" id="host_choice" ng-model="selected_host" ng-options="host.addr group by host.group for host in items" - ng-disabled="option_selected!='host'"></select> + ng-disabled="option_selected!='host'" + ng-change="get_host_subscribers()"></select> </div> </div> </div> @@ -42,7 +45,7 @@ <table class="table table-bordered"> <thead> <tr> - <th>User <button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#modal_add">Add...</button></th> + <th>User <button type="button" class="btn btn-primary btn-xs" ng-click="open_modal_add_subscriber()">Add...</button></th> <th>Notification service </th> <th>Minor notifications</th> <th>Major notifications</th> @@ -171,7 +174,24 @@ <button type="button" class="btn btn-primary">Save changes</button> </div> - <div class="modal fade" id="modal_add" tabindex="-1" role="dialog" aria-labelledby="modal_add_label" aria-hidden="true"> + <script type="text/ng-template" id="modal_add_subscriber_label.html"> + <div class="modal-header"> + <h3 class="modal-title">Add users to this group/host</h3> + </div> + <div class="modal-body"> + {{add_subscriber_args}} + <select multiple class="form-control" + ng-model="selected_users" + ng-options="user as user for user in users"> + </select> + </div> + <div class="modal-footer"> + <button type="button" class="btn btn-default" data-dismiss="modal" ng-click="cancel()">Close</button> + <button type="button" class="btn btn-primary" ng-click="ok()" ng-disabled="selected_users.length==0">Add selected</button> + </div> + </script> + + <!--<div class="modal fade" id="modal_add" tabindex="-1" role="dialog" aria-labelledby="modal_add_label" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> @@ -191,5 +211,5 @@ </div> </div> </div> - </div> + </div>--> -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.