Esempio n. 1
0
    def run(self):
        # Return All the users in the User table
        log = Logger('UserDeleteHandlerThread')
        tag = 'DELETE'

        if self.data is None:
            log.e(tag, 'No user registered in table for this user_id')
            opJson = json.dumps(
                {'pass': False,
                    'message': 'No user registered in table for this user_id'})
            self.request.write(opJson)
            tornado.ioloop.IOLoop.instance().add_callback(self.callback)

        user = UserDBHelper()
        device = DeviceDBHelper()
        enrollment = EnrollmentDBHelper()
        print 'print data here \n ... \n ', self.data

        user_list = user.get_user(str(self.data), company_id=self.company_id)
        if user_list is None:
            log.e(tag, 'No user registered in table for this user_id')
            opJson = json.dumps(
                {'pass': False,
                    'message': 'No user registered in table for this user_id'})
            self.request.write(opJson)
            tornado.ioloop.IOLoop.instance().add_callback(self.callback)

        else:
            user_deleted = user.delete_user(str(user_list.get('id')))
            if not user_deleted:
                log.e(tag, 'Not able to delete from user table')
                opJson = json.dumps(
                    {'pass': False,
                        'message': 'Not able to delete from user table'})
                self.request.write(opJson)
                tornado.ioloop.IOLoop.instance().add_callback(self.callback)
            else:

                devices = device.get_devices_of_user(str(self.data))
                if devices is not None and len(devices) > 0:
                    for each_device in devices:
                        device_id = each_device.get('id')
                        device.delete_device(str(device_id))
                        enrollment_list = enrollment.get_enrollments({
                            'device_id': device_id})
                        for enroll in enrollment_list:
                            enrollment_id = enroll.get('id')
                            enrollment.update_enrollment(
                                str(enrollment_id), {
                                    'device_id': "null", 'is_enrolled': False})
                log.i(tag, 'User delelted')
                opJson = json.dumps({'pass': True,
                                     'message': 'User Successfully deleted'})
                self.request.write(opJson)
                tornado.ioloop.IOLoop.instance().add_callback(self.callback)
Esempio n. 2
0
    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)
Esempio n. 3
0
    def run(self):
        # Return All the users in the User table
        log = Logger('UserDeleteHandlerThread')
        tag = 'DELETE'

        if self.data is None:
            log.e(tag, 'No user registered in table for this user_id')
            opJson = json.dumps({
                'pass':
                False,
                'message':
                'No user registered in table for this user_id'
            })
            self.request.write(opJson)
            tornado.ioloop.IOLoop.instance().add_callback(self.callback)

        user = UserDBHelper()
        device = DeviceDBHelper()
        enrollment = EnrollmentDBHelper()
        print 'print data here \n ... \n ', self.data

        user_list = user.get_user(str(self.data), company_id=self.company_id)
        if user_list is None:
            log.e(tag, 'No user registered in table for this user_id')
            opJson = json.dumps({
                'pass':
                False,
                'message':
                'No user registered in table for this user_id'
            })
            self.request.write(opJson)
            tornado.ioloop.IOLoop.instance().add_callback(self.callback)

        else:
            user_deleted = user.delete_user(str(user_list.get('id')))
            if not user_deleted:
                log.e(tag, 'Not able to delete from user table')
                opJson = json.dumps({
                    'pass':
                    False,
                    'message':
                    'Not able to delete from user table'
                })
                self.request.write(opJson)
                tornado.ioloop.IOLoop.instance().add_callback(self.callback)
            else:

                devices = device.get_devices_of_user(str(self.data))
                if devices is not None and len(devices) > 0:
                    for each_device in devices:
                        device_id = each_device.get('id')
                        device.delete_device(str(device_id))
                        enrollment_list = enrollment.get_enrollments(
                            {'device_id': device_id})
                        for enroll in enrollment_list:
                            enrollment_id = enroll.get('id')
                            enrollment.update_enrollment(
                                str(enrollment_id), {
                                    'device_id': "null",
                                    'is_enrolled': False
                                })
                log.i(tag, 'User delelted')
                opJson = json.dumps({
                    'pass': True,
                    'message': 'User Successfully deleted'
                })
                self.request.write(opJson)
                tornado.ioloop.IOLoop.instance().add_callback(self.callback)
Esempio n. 4
0
    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)
Esempio n. 5
0
    def command_to_user(self, json_data):
        TAG = 'command_to_user'
        print 'In command_to_user'
        user_table_id = ''
        try:
            user_table_id = str(json_data['id'])
            print user_table_id
        except:
            self.log.e(TAG,'There is something wrong in the POST parametes sent')
            ##Create the O/P JSON
            opJson = json.dumps({'pass':False,
                                 'error':'UserID not found in request'})
            self.log.e(TAG,str(opJson))
            return
        device = DeviceDBHelper()
        merger = Merger()
        print 'find device List'
        print user_table_id
        device_list = device.get_devices_of_user(user_table_id)

        print 'find device List found'
        if device_list is None:
            self.log.e(TAG, 'Device List is Empty')
            ##Create the O/P JSON
            opJson = json.dumps({'pass':False, 'error':'Device list empty'})
            self.log.e(TAG,str(opJson))
            return

        print device_list
        for device_item in device_list:
            device_os = str(device_item[c.DEVICE_TABLE_OS])
            device_id = str(device_item[c.DEVICE_TABLE_ID])
            device_udid = str(device_item[c.DEVICE_TABLE_UDID])

            if json_data.has_key('action'):
                policy_dict = {'action_command': json_data.get('action'),
                               'passcode': json_data.get('passcode')}
            elif json_data.has_key('broadcast'):
                policy_dict = {'broadcast_command': json_data.get('broadcast')}
            else:
                policy_dict = merger.merge(device_id)
            ## Now Send the parameters to the Corresponding engine
            ##Send the command to iOS Engine
            if str(device_os) == 'ios':
                print 'Now command sending to ios. Add to queue. Device id= '\
                        +device_id
                command_instance = ios_command.IosCommand()
                command_instance.execute(policy_dict, device_id, device_udid)
                #create_ios_task.delay(policy_dict, device_id, device_udid)
                opJson = json.dumps({'pass': True,
                                     'message': 'Message sent from server'})
                self.log.i(TAG,str(opJson))

            ##Send the command to Samsung Engine
            elif device_os == 'samsung':
                print 'Now command sending to samsung'
                command_instance = android_command.AndroidCommand()
                command_instance.execute(policy_dict, device_id)
                #create_android_task.delay(json_data, device_id)
                opJson = json.dumps({'pass': True,
                                     'message': 'Message sent from server'})
                self.log.i(TAG,str(opJson))

            ##Wrong device_os
            else:
                self.log.e(TAG,'Wrong Device OS in the Device Table')
                ##Create the O/P JSON
                opJson = json.dumps({'pass':False,
                                    'error':'Wrong Device OS for this device'})
                self.log.e(TAG,str(opJson))
Esempio n. 6
0
    def command_to_user(self, json_data):
        TAG = 'command_to_user'
        print 'In command_to_user'
        user_table_id = ''
        try:
            user_table_id = str(json_data['id'])
            print user_table_id
        except:
            self.log.e(
                TAG,
                'There is something wrong in the POST parametes sent')
            # Create the O/P JSON
            opJson = json.dumps({'pass': False,
                                 'error': 'UserID not found in request'})
            self.log.e(TAG, str(opJson))
            return
        device = DeviceDBHelper()
        merger = Merger()
        print 'find device List'
        print user_table_id
        device_list = device.get_devices_of_user(user_table_id)

        print 'find device List found'
        if device_list is None:
            self.log.e(TAG, 'Device List is Empty')
            # Create the O/P JSON
            opJson = json.dumps({'pass': False, 'error': 'Device list empty'})
            self.log.e(TAG, str(opJson))
            return

        print device_list
        for device_item in device_list:
            device_os = str(device_item[c.DEVICE_TABLE_OS])
            device_id = str(device_item[c.DEVICE_TABLE_ID])
            device_udid = str(device_item[c.DEVICE_TABLE_UDID])

            if 'action' in json_data:
                policy_dict = {'action_command': json_data.get('action'),
                               'passcode': json_data.get('passcode')}
            elif 'broadcast' in json_data:
                policy_dict = {'broadcast_command': json_data.get('broadcast')}
            else:
                policy_dict = merger.merge(device_id)
            # Now Send the parameters to the Corresponding engine
            # Send the command to iOS Engine
            if str(device_os) == 'ios':
                print 'Now command sending to ios. Add to queue. Device id= '\
                    + device_id
                command_instance = ios_command.IosCommand()
                command_instance.execute(policy_dict, device_id, device_udid)
                #create_ios_task.delay(policy_dict, device_id, device_udid)
                opJson = json.dumps({'pass': True,
                                     'message': 'Message sent from server'})
                self.log.i(TAG, str(opJson))

            # Send the command to Samsung Engine
            elif device_os == 'samsung':
                print 'Now command sending to samsung'
                command_instance = android_command.AndroidCommand()
                command_instance.execute(policy_dict, device_id)
                #create_android_task.delay(json_data, device_id)
                opJson = json.dumps({'pass': True,
                                     'message': 'Message sent from server'})
                self.log.i(TAG, str(opJson))

            # Wrong device_os
            else:
                self.log.e(TAG, 'Wrong Device OS in the Device Table')
                # Create the O/P JSON
                opJson = json.dumps(
                    {'pass': False,
                        'error': 'Wrong Device OS for this device'})
                self.log.e(TAG, str(opJson))