def run(self): log = Logger('RolePolicyGetHandlerThread') TAG = 'run' if self.data and self.company_id: # do stuff here role = RoleDBHelper() url_data = self.data.split('/') role_id = url_data[0] plugin_name = url_data[1] role_detail = role.get_role(str(role_id), company_id=self.company_id) if role_detail: role_policy_id = role_detail.get('policy_id') if plugin_name == 'actions': action_command = True else: action_command = False if role_policy_id and not action_command: plugin_data = get_individual_plugin(role_policy_id, plugin_name) elif not action_command: role_policy_id, plugin_data = setup_default_policy() if role_policy_id: role.set_role_policy(str(role_id), str(role_policy_id)) plugin_data = plugin_data.get(plugin_name) else: log.e(TAG, 'Role Policy ID setup failed.') opJson = json.dumps({'pass': False, 'count':0, 'message': 'Role policy creation failed.'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback( self.callback) else: plugin_data = {} if isinstance(plugin_data, str): plugin_data = json.loads(plugin_data) plugin_data['_id'] = role_policy_id plugin_data['object_type'] = 'Role' plugin_data['name'] = role_detail.get(c.ROLE_TABLE_NAME) opJson = json.dumps({'count':1, 'message': 'Successfull', 'data':plugin_data, 'pass': True}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) else: log.e(TAG, 'No valid role id is sent in request') opJson = json.dumps({'pass': False, 'message': 'No valid role id is sent in request'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) else: log.e(TAG, 'UnAuthorized Access for role policy ') self.request.set_status(401) tornado.ioloop.IOLoop.instance().add_callback(self.callback)
def run(self): log = Logger('RolePolicyPutHandlerThread') TAG = 'run' # Flag to track status of policy status = False if self.data: # do stuff here role = RoleDBHelper() url_data = self.data.split('/') print self.request.request.body request_data = json.loads(self.request.request.body) role_id = url_data[0] plugin_name = url_data[1] role_detail = role.get_role(str(role_id), company_id=self.company_id) if role_detail: role_policy_id = role_detail.get('policy_id') command_handler_json = {'to': 'role', 'id': str(role_id), 'company_id': self.company_id} if plugin_name != 'actions': status = put_individual_plugin(role_policy_id, plugin_name, request_data) else: status = True command_handler_json['action'] = request_data.get('action') command_handler_json['passcode'] = request_data.get('lock_key') if status: print "\nprinting the json output will be send to command\ handler\n", command_handler_json create_command_handler_task.delay(command_handler_json) request_data['_id'] = role_policy_id request_data['object_type'] = 'Role' request_data['name'] = role_detail.get(c.ROLE_TABLE_NAME) opJson = json.dumps({'data': request_data, 'pass': True, 'count': 1, 'message': 'Everything fine'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) else: opJson = json.dumps({'pass': False, 'count':0, 'message': 'Update operation at policy table failed'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) else: log.e(TAG, 'No valid role id is sent in request') opJson = json.dumps({'pass': False, 'message': 'No valid role id is sent in request'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) else: log.e(TAG, 'UnAuthorized Access for role policy ') self.request.set_status(401) tornado.ioloop.IOLoop.instance().add_callback(self.callback)
def run(self): log = Logger('RoleGetHandlerThread') TAG = 'GET' page = self.request.get_argument('offset', None) count = self.request.get_argument('count', None) sort_by = self.request.get_argument('sort_by', None) print page print count print sort_by if self.data and self.company_id: role = RoleDBHelper() roles = role.get_role(str(self.data), company_id=self.company_id) outer_dict = {} outer_array = [] if roles is None: log.e(TAG, 'No role found corresponding to the role id ' + \ str(self.data)) opJson = json.dumps({'pass': False, 'message':'No role found \ corresponding to the role id ' + str(self.data)}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) else: self.role_name = roles[c.ROLE_TABLE_NAME] user = UserDBHelper() filter_dict = { c.USER_TABLE_ROLE: str(roles.get('id')) } user_list = user.get_users_with_pages(filter_dict, int(page), int(count), str(sort_by)) inner_array = [] if user_list is not None: for users in user_list: inner_dict = {} self.user_id = str(users[c.USER_TABLE_ID]) self.user_name = str(users[c.USER_TABLE_NAME]) self.user_email = str(users[c.USER_TABLE_EMAIL]) self.user_team_id = str(users[c.USER_TABLE_TEAM]) ## Find out the team for user team = TeamDBHelper() teams = team.get_team(self.user_team_id, [c.TEAM_TABLE_NAME]) if teams is None: self.user_team = None else: self.user_team = str(teams[c.TEAM_TABLE_NAME]) ## Find out the device for user device = DeviceDBHelper() device_list = device.get_devices_of_user(self.user_id) if device_list is None: self.user_device = None else: devices = device_list[0] self.user_device = str( devices[c.DEVICE_TABLE_UDID]) self.user_device_os = str( devices[c.DEVICE_TABLE_OS]) # Find out user violations violation = ViolationsDBHelper() violation_count = violation.get_violation_count( str(devices.get('id'))) inner_dict = { 'user_id': self.user_id, 'user_name': self.user_name, 'user_role': self.role_name, 'user_team': self.user_team, 'user_device': self.user_device, 'user_violations': violation_count, 'user_device_os': self.user_device_os } inner_array.append(inner_dict) outer_dict['name'] = self.role_name outer_dict['users'] = inner_array outer_dict['roleID'] = self.data outer_array.append(outer_dict) self.final_dict['pass'] = True self.final_dict['roles'] = outer_array opJson = json.dumps(self.final_dict) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) else: log.e(TAG, 'UnAuthorized Access for Roles') self.request.set_status(401) tornado.ioloop.IOLoop.instance().add_callback(self.callback)
def merge(self, device_id): user_helper = UserDBHelper() device_helper = DeviceDBHelper() roles_helper = RoleDBHelper() teams_helper = TeamDBHelper() company_helper = CompanyDBHelper() policy_helper = PolicyDBHelper() if device_id is not None: device_details = device_helper.get_device(device_id) if device_details is not None and 'user_id' in device_details: user_details = user_helper.get_user( str(device_details['user_id'])) team_id = user_details['team_id'] role_id = user_details['role_id'] company_id = user_details['company_id'] team_details = teams_helper.get_team(str(team_id)) role_details = roles_helper.get_role(str(role_id)) company_details = company_helper.get_company(str(company_id)) if user_details is not None and 'policy_id' in user_details: policy_id_user = user_details['policy_id'] else: print 'No user details found' if team_details is not None and 'policy_id' in team_details: policy_id_team = team_details['policy_id'] else: print 'no team details found' if role_details is not None and 'policy_id' in role_details: policy_id_role = role_details['policy_id'] else: print 'no role details found' if (company_details is not None and 'policy_id' in company_details): policy_id_company = company_details['policy_id'] else: print 'no company details found' if policy_id_company is not None: print 'company policy id=', policy_id_company policy_company = policy_helper.get_policy( str(policy_id_company)) else: policy_company = None if policy_id_role is not None: print 'role policy id=', policy_id_role policy_role = policy_helper.get_policy(str(policy_id_role)) else: policy_role = None if policy_id_team is not None: print 'team policy id=', policy_id_team policy_team = policy_helper.get_policy(str(policy_id_team)) else: policy_team = None if policy_id_user is not None: print 'user policy id=', policy_id_user policy_user = policy_helper.get_policy(str(policy_id_user)) else: policy_user = None return self.merge_policies( policy_company, policy_role, policy_team, policy_user) else: print 'Invalid device id'
def run(self): log = Logger('RolePolicyPutHandlerThread') TAG = 'run' # Flag to track status of policy status = False if self.data: # do stuff here role = RoleDBHelper() url_data = self.data.split('/') print self.request.request.body request_data = json.loads(self.request.request.body) role_id = url_data[0] plugin_name = url_data[1] role_detail = role.get_role(str(role_id), company_id=self.company_id) if role_detail: role_policy_id = role_detail.get('policy_id') command_handler_json = {'to': 'role', 'id': str(role_id), 'company_id': self.company_id} if plugin_name != 'actions': status = put_individual_plugin(role_policy_id, plugin_name, request_data) else: status = True command_handler_json['action'] = request_data.get('action') command_handler_json[ 'passcode'] = request_data.get('lock_key') if status: print "\nprinting the json output will be send to command\ handler\n", command_handler_json create_command_handler_task.delay(command_handler_json) request_data['_id'] = role_policy_id request_data['object_type'] = 'Role' request_data['name'] = role_detail.get(c.ROLE_TABLE_NAME) opJson = json.dumps( {'data': request_data, 'pass': True, 'count': 1, 'message': 'Everything fine'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback( self.callback) else: opJson = json.dumps( {'pass': False, 'count': 0, 'message': 'Update operation at policy table \ failed'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback( self.callback) else: log.e(TAG, 'No valid role id is sent in request') opJson = json.dumps( {'pass': False, 'message': 'No valid role id is sent in request'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) else: log.e(TAG, 'UnAuthorized Access for role policy ') self.request.set_status(401) tornado.ioloop.IOLoop.instance().add_callback(self.callback)
def run(self): log = Logger('RolePolicyGetHandlerThread') TAG = 'run' if self.data and self.company_id: # do stuff here role = RoleDBHelper() url_data = self.data.split('/') role_id = url_data[0] plugin_name = url_data[1] role_detail = role.get_role(str(role_id), company_id=self.company_id) if role_detail: role_policy_id = role_detail.get('policy_id') if plugin_name == 'actions': action_command = True else: action_command = False if role_policy_id and not action_command: plugin_data = get_individual_plugin(role_policy_id, plugin_name) elif not action_command: role_policy_id, plugin_data = setup_default_policy() if role_policy_id: role.set_role_policy(str(role_id), str(role_policy_id)) plugin_data = plugin_data.get(plugin_name) else: log.e(TAG, 'Role Policy ID setup failed.') opJson = json.dumps( {'pass': False, 'count': 0, 'message': 'Role policy creation failed.'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback( self.callback) else: plugin_data = {} if isinstance(plugin_data, str): plugin_data = json.loads(plugin_data) plugin_data['_id'] = role_policy_id plugin_data['object_type'] = 'Role' plugin_data['name'] = role_detail.get(c.ROLE_TABLE_NAME) opJson = json.dumps({'count': 1, 'message': 'Successfull', 'data': plugin_data, 'pass': True}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) else: log.e(TAG, 'No valid role id is sent in request') opJson = json.dumps( {'pass': False, 'message': 'No valid role id is sent in request'}) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) else: log.e(TAG, 'UnAuthorized Access for role policy ') self.request.set_status(401) tornado.ioloop.IOLoop.instance().add_callback(self.callback)
def run(self): log = Logger('TeamGetHandlerThread') TAG = 'GET' page = self.request.get_argument('page', None) count = self.request.get_argument('count', None) sort_by = self.request.get_argument('sort_by', None) if self.data is None: log.e(TAG, 'No Team ID in Request') opJson = json.dumps({ 'pass': False, 'message': 'No team found corresponding to the team \ id ' + str(self.data) }) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) else: team = TeamDBHelper() teams = team.get_team(str(self.data), company_id=self.company_id) outer_dict = {} outer_array = [] print teams if teams is None: log.e( TAG, 'No team found corresponding to the team id ' + str(self.data)) opJson = json.dumps({ 'pass': False, 'message': 'No team found corresponding to the team \ id ' + str(self.data) }) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback) else: self.team_name = teams.get(c.TEAM_TABLE_NAME) user = UserDBHelper() filter_dict = {c.USER_TABLE_TEAM: str(teams.get('id'))} user_list = user.get_users_with_pages(filter_dict, int(page), int(count), str(sort_by)) inner_array = [] if user_list is not None: for users in user_list: inner_dict = {} self.user_id = str(users[c.USER_TABLE_ID]) self.user_name = str(users[c.USER_TABLE_NAME]) self.user_email = str(users[c.USER_TABLE_EMAIL]) self.user_role_id = str(users[c.USER_TABLE_ROLE]) # Find out the role for user role = RoleDBHelper() roles = role.get_role(self.user_role_id, [c.ROLE_TABLE_NAME]) if roles is None: self.user_role = None else: self.user_role = str(roles[c.ROLE_TABLE_NAME]) # Find out the device for user device = DeviceDBHelper() device_list = device.get_devices_of_user(self.user_id) if device_list is None: self.user_device = None else: devices = device_list[0] self.user_device = str( devices[c.DEVICE_TABLE_UDID]) self.user_device_os = str( devices[c.DEVICE_TABLE_OS]) # Find out user violations violation = ViolationsDBHelper() violation_count = violation.get_violation_count( str(devices.get('id'))) inner_dict = { 'user_id': self.user_id, 'user_name': self.user_name, 'user_role': self.user_role, 'user_team': self.team_name, 'user_device': self.user_device, 'user_violations': violation_count, 'user_device_os': self.user_device_os, 'user_email': self.user_email } inner_array.append(inner_dict) outer_dict['name'] = self.team_name outer_dict['users'] = inner_array outer_dict['team_id'] = self.data print '\nouter_dict' print outer_dict outer_array.append(outer_dict) self.final_dict['pass'] = True self.final_dict['teams'] = outer_array opJson = json.dumps(self.final_dict) self.request.write(opJson) tornado.ioloop.IOLoop.instance().add_callback(self.callback)
def perform(self): TAG = 'run' print 'In RuN' self.insert = True json_data = self.data print json_data['to'] #Find out the category of the Device on the basis of 'to' field # Case 1 : Command is sent to the USER if str(json_data['to']) == 'user': to_id = str(json_data['id']) company_id = json_data.get('company_id') user = UserDBHelper() user_dict = user.get_user(str(to_id), company_id=company_id, pluck=[c.USER_TABLE_NAME]) if user_dict is not None: user_name = str(user_dict[c.USER_TABLE_NAME]) message = "Command sent to " + user_name +\ " having ID = " + str(json_data['id']) logs = LogsDBHelper() logs_id = logs.add_log(to_id, str(json_data['to']), 'info', None, message, raw=None, company=str(company_id)) if logs_id is None: self.log.e(TAG, 'Not able to insert the logs') self.command_to_user(json_data) else: self.log.e(TAG, 'No details corresponding to user found') #Case 2: Command is sent to the Teams elif str(json_data['to']) == 'team': print 'sending to teams' team_id = str(json_data['id']) company_id = json_data.get('company_id') team = TeamDBHelper() team_dict = team.get_team(str(team_id), company_id=company_id, pluck=[c.TEAM_TABLE_NAME]) if team_dict is not None: team_name = str(team_dict[c.TEAM_TABLE_NAME]) message = "Command sent to " + team_name +\ " having ID = " + str(json_data['id']) logs = LogsDBHelper() logs_id = logs.add_log(team_id, str(json_data['to']), 'info', None, message, raw=None, company=str(company_id)) if logs_id is None: self.log.e(TAG, 'Not able to insert the logs') self.command_to_team(json_data) else: self.log.e(TAG, "No details corresponding to team_id found. ") #Case 3: Command is sent to the Role elif str(json_data['to']) == 'role': role_id = str(json_data['id']) company_id = json_data.get('company_id') role = RoleDBHelper() role_dict = role.get_role(str(role_id), company_id=company_id, pluck=[c.ROLE_TABLE_NAME]) if role_dict is not None: role_name = str(role_dict[c.ROLE_TABLE_NAME]) message = "Command sent to " + role_name +\ " having ID = " + str(json_data['id']) logs = LogsDBHelper() logs_id = logs.add_log(role_id, str(json_data['to']), 'info', None, message, raw=None, company=str(company_id)) if logs_id is None: self.log.e(TAG, 'Not able to insert the logs') self.command_to_role(json_data) else: self.log.e(TAG, 'No role corresponding to given role_id found') elif str(json_data['to']) == 'company': company_id = str(json_data['id']) company = CompanyDBHelper() company_dict = company.get_company(str(company_id)) if company_dict is not None: company_name = str(company_dict[c.COMPANY_TABLE_NAME]) message = "Command sent to " + company_name\ +" having ID = " + str(json_data['id']) logs = LogsDBHelper() logs_id = logs.add_log(company_id, str(json_data['to']), 'info', None, message, raw=None, company=company_id) if logs_id is None: self.log.e(TAG, 'Not able to insert the logs') self.command_to_company(json_data) else: self.log.e(TAG, 'No data corresponding to team id given found') #Case 5: Some other parameter sent in 'to' field else: self.log.e(TAG, 'Somthing wrong with \'to\' field of POST data') ##Create the O/P JSON opJson = json.dumps({'pass':False, 'error':'Correct TO field'}) self.log.e(TAG, str(opJson))
def merge(self, device_id): user_helper = UserDBHelper() device_helper = DeviceDBHelper() roles_helper = RoleDBHelper() teams_helper = TeamDBHelper() company_helper = CompanyDBHelper() policy_helper = PolicyDBHelper() if device_id is not None: device_details = device_helper.get_device(device_id) if device_details is not None and 'user_id' in device_details: user_details = user_helper.get_user( str(device_details['user_id'])) team_id = user_details['team_id'] role_id = user_details['role_id'] company_id = user_details['company_id'] team_details = teams_helper.get_team(str(team_id)) role_details = roles_helper.get_role(str(role_id)) company_details = company_helper.get_company(str(company_id)) if user_details is not None and 'policy_id' in user_details: policy_id_user = user_details['policy_id'] else: print 'No user details found' if team_details is not None and 'policy_id' in team_details: policy_id_team = team_details['policy_id'] else: print 'no team details found' if role_details is not None and 'policy_id' in role_details: policy_id_role = role_details['policy_id'] else: print 'no role details found' if (company_details is not None and 'policy_id' in company_details): policy_id_company = company_details['policy_id'] else: print 'no company details found' if policy_id_company is not None: print 'company policy id=', policy_id_company policy_company = policy_helper.get_policy( str(policy_id_company)) else: policy_company = None if policy_id_role is not None: print 'role policy id=', policy_id_role policy_role = policy_helper.get_policy(str(policy_id_role)) else: policy_role = None if policy_id_team is not None: print 'team policy id=', policy_id_team policy_team = policy_helper.get_policy(str(policy_id_team)) else: policy_team = None if policy_id_user is not None: print 'user policy id=', policy_id_user policy_user = policy_helper.get_policy(str(policy_id_user)) else: policy_user = None return self.merge_policies(policy_company, policy_role, policy_team, policy_user) else: print 'Invalid device id'
def perform(self): TAG = 'run' print 'In RuN' self.insert = True json_data = self.data print json_data['to'] # Find out the category of the Device on the basis of 'to' field # Case 1 : Command is sent to the USER if str(json_data['to']) == 'user': to_id = str(json_data['id']) company_id = json_data.get('company_id') user = UserDBHelper() user_dict = user.get_user(str(to_id), company_id=company_id, pluck=[c.USER_TABLE_NAME]) if user_dict is not None: user_name = str(user_dict[c.USER_TABLE_NAME]) message = "Command sent to " + user_name +\ " having ID = " + str(json_data['id']) logs = LogsDBHelper() logs_id = logs.add_log(to_id, str(json_data['to']), 'info', None, message, raw=None, company=str(company_id)) if logs_id is None: self.log.e(TAG, 'Not able to insert the logs') self.command_to_user(json_data) else: self.log.e(TAG, 'No details corresponding to user found') # Case 2: Command is sent to the Teams elif str(json_data['to']) == 'team': print 'sending to teams' team_id = str(json_data['id']) company_id = json_data.get('company_id') team = TeamDBHelper() team_dict = team.get_team(str(team_id), company_id=company_id, pluck=[c.TEAM_TABLE_NAME]) if team_dict is not None: team_name = str(team_dict[c.TEAM_TABLE_NAME]) message = "Command sent to " + team_name +\ " having ID = " + str(json_data['id']) logs = LogsDBHelper() logs_id = logs.add_log(team_id, str(json_data['to']), 'info', None, message, raw=None, company=str(company_id)) if logs_id is None: self.log.e(TAG, 'Not able to insert the logs') self.command_to_team(json_data) else: self.log.e(TAG, "No details corresponding to team_id found. ") # Case 3: Command is sent to the Role elif str(json_data['to']) == 'role': role_id = str(json_data['id']) company_id = json_data.get('company_id') role = RoleDBHelper() role_dict = role.get_role(str(role_id), company_id=company_id, pluck=[c.ROLE_TABLE_NAME]) if role_dict is not None: role_name = str(role_dict[c.ROLE_TABLE_NAME]) message = "Command sent to " + role_name +\ " having ID = " + str(json_data['id']) logs = LogsDBHelper() logs_id = logs.add_log(role_id, str(json_data['to']), 'info', None, message, raw=None, company=str(company_id)) if logs_id is None: self.log.e(TAG, 'Not able to insert the logs') self.command_to_role(json_data) else: self.log.e(TAG, 'No role corresponding to given role_id found') elif str(json_data['to']) == 'company': company_id = str(json_data['id']) company = CompanyDBHelper() company_dict = company.get_company(str(company_id)) if company_dict is not None: company_name = str(company_dict[c.COMPANY_TABLE_NAME]) message = "Command sent to " + company_name\ + " having ID = " + str(json_data['id']) logs = LogsDBHelper() logs_id = logs.add_log(company_id, str(json_data['to']), 'info', None, message, raw=None, company=company_id) if logs_id is None: self.log.e(TAG, 'Not able to insert the logs') self.command_to_company(json_data) else: self.log.e(TAG, 'No data corresponding to team id given found') # Case 5: Some other parameter sent in 'to' field else: self.log.e(TAG, 'Somthing wrong with \'to\' field of POST data') # Create the O/P JSON opJson = json.dumps({'pass': False, 'error': 'Correct TO field'}) self.log.e(TAG, str(opJson))