Beispiel #1
0
    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)
Beispiel #2
0
    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)
Beispiel #3
0
    def run(self):
        log = Logger('RolesDeleteHandlerThread')
        TAG = 'DELETE'

        if self.data is None:
            log.e(TAG, 'No Role ID in Request')
            self.request.set_status(401)
            tornado.ioloop.IOLoop.instance().add_callback(self.callback)
        else:
            role = RoleDBHelper()
            is_deleted = role.delete_role(str(self.data), self.company_id)
            if is_deleted:
                log.i(TAG, 'Role Successfully deleted')
                opJson = json.dumps({'pass': True,
                        'message': 'Role Successfully deleted'})
                self.request.write(opJson)
                tornado.ioloop.IOLoop.instance().add_callback(self.callback)
            else:
                log.e(TAG, 'Role Not Deleted')
                opJson = json.dumps({'pass': False,
                        'message': 'Role Not Deleted'})
                self.request.write(opJson)
                tornado.ioloop.IOLoop.instance().add_callback(self.callback)
Beispiel #4
0
    def run(self):
        log = Logger('RolesDeleteHandlerThread')
        TAG = 'DELETE'

        if self.data is None:
            log.e(TAG, 'No Role ID in Request')
            self.request.set_status(401)
            tornado.ioloop.IOLoop.instance().add_callback(self.callback)
        else:
            role = RoleDBHelper()
            is_deleted = role.delete_role(str(self.data), self.company_id)
            if is_deleted:
                log.i(TAG, 'Role Successfully deleted')
                opJson = json.dumps({'pass': True,
                                     'message': 'Role Successfully deleted'})
                self.request.write(opJson)
                tornado.ioloop.IOLoop.instance().add_callback(self.callback)
            else:
                log.e(TAG, 'Role Not Deleted')
                opJson = json.dumps({'pass': False,
                                     'message': 'Role Not Deleted'})
                self.request.write(opJson)
                tornado.ioloop.IOLoop.instance().add_callback(self.callback)
Beispiel #5
0
    def run(self):
        #Return All the users in the User table

        company_id = self.company_id

        log = Logger('RolesPostHandler')
        tag = 'run'
        request_body = json.loads(self.request.request.body)

        self.user_role = request_body.get('name')

        if self.user_role is None or self.user_role == '':
            log.e(tag, 'sent Role is either None or blank')
            opJson = json.dumps({'pass': False,
                                 'error': 'sent Role is either None or blank'})
            self.request.write(opJson)
            tornado.ioloop.IOLoop.instance().add_callback(self.callback)

        role = RoleDBHelper()
        insert_dict = {
                      c.ROLE_TABLE_NAME: self.user_role,
                      c.ROLE_TABLE_COMPANY: company_id,
                      c.ROLE_TABLE_DELETED: False
                      }

        self.user_role_id = role.add_role(insert_dict)

        if self.user_role_id is None:
            log.e(tag,'Role not inserted in table')
            opJson = json.dumps({'pass': False,
                                 'error': 'Role not inserted in table'})
            self.request.write(opJson)
            tornado.ioloop.IOLoop.instance().add_callback(self.callback)
        else:
            opJson = json.dumps({'pass': True, 'role_id': self.user_role_id})
            self.request.write(opJson)
            tornado.ioloop.IOLoop.instance().add_callback(self.callback)
Beispiel #6
0
    def run(self):
        # Return All the users in the User table

        company_id = self.company_id

        log = Logger('RolesPostHandler')
        tag = 'run'
        request_body = json.loads(self.request.request.body)

        self.user_role = request_body.get('name')

        if self.user_role is None or self.user_role == '':
            log.e(tag, 'sent Role is either None or blank')
            opJson = json.dumps({'pass': False,
                                 'error': 'sent Role is either None or blank'})
            self.request.write(opJson)
            tornado.ioloop.IOLoop.instance().add_callback(self.callback)

        role = RoleDBHelper()
        insert_dict = {
            c.ROLE_TABLE_NAME: self.user_role,
            c.ROLE_TABLE_COMPANY: company_id,
            c.ROLE_TABLE_DELETED: False
        }

        self.user_role_id = role.add_role(insert_dict)

        if self.user_role_id is None:
            log.e(tag, 'Role not inserted in table')
            opJson = json.dumps({'pass': False,
                                 'error': 'Role not inserted in table'})
            self.request.write(opJson)
            tornado.ioloop.IOLoop.instance().add_callback(self.callback)
        else:
            opJson = json.dumps({'pass': True, 'role_id': self.user_role_id})
            self.request.write(opJson)
            tornado.ioloop.IOLoop.instance().add_callback(self.callback)
Beispiel #7
0
    def run(self):
        # Return All the users in the User table
        log = Logger('TeamsGetHandlerThread')
        tag = 'run'
        print 'In TeamsGetHandlerThread'
        final_dict = {}

        role = RoleDBHelper()
        team = TeamDBHelper()
        user = UserDBHelper()

        team_name = self.request.get_argument('team', None)
        offset = self.request.get_argument('offset', None)
        count = self.request.get_argument('count', None)
        sort_by = self.request.get_argument(
            'sort_by',
            True)  # Intentionally done
        name_query = self.request.get_argument('name', None)
        role_query = self.request.get_argument('role', None)
        device_query = self.request.get_argument('device_id', None)
        sort_order = self.request.get_argument('sort', None)
        filter_key = str(self.request.get_argument('filter_key', None))
        filter_value = self.request.get_argument('filter_value', None)

        # Find all the roles in the Roles Table

        if filter_key == 'role':
            role_name = filter_value
            role_id = role.get_role_by_name(str(role_name), self.company_id)
        else:
            role_name = None
            role_id = None

        if name_query:
            query = name_query
            query_type = 'name'
        elif role_query:
            query = role_query
            query_type = 'role'
        elif device_query:
            query = device_query
            query_type = 'device'
        else:
            query = None
            query_type = None

        if team_name:
            team_id = team.get_team_by_name(str(team_name), self.company_id)
        else:
            team_id = None
            teams_list = team.get_teams(self.company_id, [c.TEAM_TABLE_NAME,
                                                          c.TEAM_TABLE_ID])

        if team_id:
            result_list, total_count = user.get_users_for_team(
                team_name=team_name, team_id=team_id,
                role_name=role_name, role_id=role_id,
                offset=offset, count=count, sort_by=sort_by,
                query=query, query_type=query_type,
                sort_order=sort_order)

        elif teams_list:
            for _team in teams_list:
                _team['team_type'] = _team.get('name')
                _team['team_name'] = _team.get('name')
                _team['team_id'] = _team.get('id')
                del _team['name']
                del _team['id']
            result_list = teams_list
            total_count = len(result_list)

        else:
            result_list = None

        if result_list:
            print "\n total", total_count
            final_dict['data'] = result_list
            final_dict['count'] = total_count
            final_dict['pass'] = True
            final_dict['message'] = 'Success ...'
        else:
            final_dict['pass'] = True
            final_dict['data'] = []
            log.e(tag, 'No Team in Team Table')
            final_dict['message'] = 'Failed ...'
            final_dict['count'] = 0

        opJson = json.dumps(final_dict)
        self.request.write(opJson)
        tornado.ioloop.IOLoop.instance().add_callback(self.callback)
Beispiel #8
0
    def run(self):
        #Return All the users in the User table
        log = Logger('UsersGetHandlerThreadWithPage')
        tag = 'run'

        company_id = self.company_id

        final_dict = {}

        user = UserDBHelper()
        team = TeamDBHelper()
        role = RoleDBHelper()
        violation = ViolationsDBHelper()
        device = DeviceDBHelper()

        offset = self.request.get_argument('offset', None)
        count = self.request.get_argument('count', None)
        name_query = self.request.get_argument('name', None)
        team_query = self.request.get_argument('team', None)
        role_query = self.request.get_argument('role', None)
        device_query = self.request.get_argument('device_id', None)
        sort_by = self.request.get_argument('sort_by', True)#Intentionally done
        sort_order = self.request.get_argument('sort', None)
        filter_key = self.request.get_argument('filter_key', None)
        filter_value = self.request.get_argument('filter_value', None)

        if filter_key == 'role':
            role_name = filter_value
            role_id = role.get_role_by_name(str(role_name), company_id)
        else:
            role_name = None
            role_id = None

        if filter_key == 'team':
            team_name = filter_value
            team_id = team.get_team_by_name(str(team_name), company_id)
        else:
            team_name = None
            team_id = None

        if filter_key == 'os':
            os_mapper = {'Android': 'samsung', 'iOS': 'ios'}
            os_type = os_mapper.get(str(filter_value))
        else:
            os_type = None

        if name_query:
            query  = name_query
            query_type = 'name'
        elif role_query:
            query = role_query
            query_type = 'role'
        elif device_query:
            query = device_query
            query_type = 'device'
        elif team_query:
            query = team_query
            query_type = 'team'
        else:
            query = None
            query_type = None

        if offset:
            result_list, total_count = user.get_users_for_user(
                        company_id=company_id, offset=offset, count=count,
                        role_id=role_id, team_id=team_id, query=query,
                        query_type=query_type, os_type=os_type,
                        sort_by=sort_by, sort_order=sort_order,
                        filter_key=filter_key, filter_value=filter_value)

        else:
            result_list, total_count = user.get_users(
                                    {c.USER_TABLE_COMPANY: company_id})

        if result_list:
            for user_dict in result_list:
                device_deleted = False
                device_info = device.get_device_with_udid(user_dict.get(
                                    'user_device'))
                if not device_info:
                    device_info = device.get_device_with_udid(user_dict.get(
                                    'user_device'), status=True)
                    device_deleted = True
                if device_info:
                    device_id = device_info[0].get(c.DEVICE_TABLE_ID)
                else:
                    device_id = None

                if device_deleted:
                    user_dict['user_device'] = None
                    user_dict['user_device_os'] = None

                if device_id:
                    print "printing \n device id", device_id
                    violation_count = violation.get_violation_count(
                          company_id=company_id, device_id=str(device_id))
                else:
                    violation_count = 0
                user_dict['user_violation'] = violation_count

            final_dict['data'] = result_list
            final_dict['count'] = total_count
            final_dict['pass'] = True
            final_dict['message'] = 'Seems like things are working ...'

        else:
            final_dict['pass'] = True
            log.e(tag,'No User in User Table')
            final_dict['message'] = 'Seems like things are not working ...'
            final_dict['count'] = 0

        ## add all the data into dictionary and create output json
        opJson = json.dumps(final_dict)
        self.request.write(opJson)
        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'
Beispiel #10
0
    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)
Beispiel #11
0
    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)
Beispiel #12
0
    def run(self):
        log = Logger('RolesPutHandlerThread')
        TAG = 'PUT'

        input_dict = json.loads(self.request.request.body)

        self.role_id = input_dict.get('role_id')
        if self.role_id is None:
            log.e(TAG, 'No role_id sent aborting update')
            opJson = json.dumps({'pass': False, 'message': 'No role_id sent'})
            self.request.set_status(401)
            self.request.write(opJson)
            tornado.ioloop.IOLoop.instance().add_callback(self.callback)

        else:
            if input_dict.has_key('name'):
                print 'Name found'
                self.role_name = input_dict['name']
                if self.role_name is None:
                    log.e(TAG, 'No role_name sent aborting update')
                    opJson = json.dumps({'pass': False, 'message': 'No role_name sent'})
                    self.request.write(opJson)
                    tornado.ioloop.IOLoop.instance().add_callback(self.callback)
                else:
                    role = RoleDBHelper()
                    update_dict = {
                                   c.ROLE_TABLE_NAME: self.role_name
                                   }
                    result = role.update_role(str(self.role_id),
                           self.company_id, update_dict)
                    if result == False:
                        log.e(TAG, 'Not able to update the role_name at DB')
                        opJson = json.dumps({'pass': False, 'message': 'Not able to update the role_name at DB'})
                        self.request.write(opJson)
                        tornado.ioloop.IOLoop.instance().add_callback(self.callback)
                    else:
                        log.i(TAG, 'role_name Updated successfully')
                        opJson = json.dumps({'pass': True, 'message': 'role_name updated for role.'})
                        self.request.write(opJson)
                        tornado.ioloop.IOLoop.instance().add_callback(self.callback)

            elif input_dict.has_key('policy_id'):
                print 'Policy found'
                self.role_policy_id = input_dict['policy_id']
                if self.role_policy_id is None:
                    log.e(TAG, 'No Policy ID sent aborting update')
                    opJson = json.dumps({'pass': False, 'message': 'No policy_id sent'})
                    self.request.write(opJson)
                    tornado.ioloop.IOLoop.instance().add_callback(self.callback)
                else:
                    role = RoleDBHelper()
                    update_dict = {
                                   c.ROLE_TABLE_POLICY: self.role_policy_id
                                   }
                    result = role.update_role(str(self.role_id),
                                 self.company_id, update_dict)
                    if result == False:
                        log.e(TAG, 'Not able to update the policy_id at DB')
                        opJson = json.dumps({'pass': False, 'message': 'Not able to update the policy_id at DB'})
                        self.request.write(opJson)
                        tornado.ioloop.IOLoop.instance().add_callback(self.callback)
                    else:
                        log.i(TAG, 'policy_id Updated successfully')
                        opJson = json.dumps({'pass': True, 'message': 'policy_id updated for role.'})
                        self.request.write(opJson)
                        tornado.ioloop.IOLoop.instance().add_callback(self.callback)
Beispiel #13
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)
    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))
Beispiel #15
0
    def parse(self,
              excel_path,
              company,
              company_name=None,
              callback=None,
              user_added_callback=None):
        COMPANY = company
        company_name = company_name
        book = open_workbook(filename=excel_path)
        sheet = book.sheet_by_index(0)

        users = list()
        for row_index in range(sheet.nrows):
            user = dict()
            for col_index in range(sheet.ncols):
                cell = sheet.cell(row_index, col_index).value
                if col_index == 0:
                    user[C.USER_TABLE_EMAIL] = cell
                elif col_index == 1:
                    user[C.USER_TABLE_NAME] = cell
                elif col_index == 2:
                    user[C.USER_TABLE_TEAM] = cell
                elif col_index == 3:
                    user[C.USER_TABLE_ROLE] = cell
            users.append(user)
        roles_list = list()
        teams_list = list()
        for user in users:
            if C.USER_TABLE_ROLE in user:
                roles_list.append(user[C.USER_TABLE_ROLE])
            if C.USER_TABLE_TEAM in user:
                teams_list.append(user[C.USER_TABLE_TEAM])

        # get a set to get the unique elements.
        roles = dict()
        teams = dict()

        roles_helper = RoleDBHelper()
        teams_helper = TeamDBHelper()
        users_helper = UserDBHelper()
        enrollment_helper = EnrollmentDBHelper()

        for role_name in set(roles_list):
            role = dict()
            role[C.ROLE_TABLE_COMPANY] = COMPANY
            role[C.ROLE_TABLE_NAME] = role_name
            roles[role_name] = roles_helper.add_role(role)

        for team_name in set(teams_list):
            team = dict()
            team[C.TEAM_TABLE_COMPANY] = COMPANY
            team[C.TEAM_TABLE_NAME] = team_name
            team[C.TEAM_TABLE_DELETED] = False
            teams[team_name] = teams_helper.add_team(team)

        # Now we have id for all teams and roles... Insert the users now.
        for user in users:
            user_obj = dict()
            user_obj[C.USER_TABLE_COMPANY] = COMPANY
            user_obj[C.USER_TABLE_EMAIL] = user[C.USER_TABLE_EMAIL]
            user_obj[C.USER_TABLE_NAME] = user[C.USER_TABLE_NAME]
            user_obj[C.USER_TABLE_ROLE] = roles[user[C.USER_TABLE_ROLE]]
            user_obj[C.USER_TABLE_TEAM] = teams[user[C.USER_TABLE_TEAM]]
            user['id'] = users_helper.add_user_if_not_exists(user_obj)[0]
            if user_added_callback is not None:
                user_added_callback(user_obj)

        for user in users:
            enrollment = dict()
            enrollment[C.ENROLLMENT_TABLE_USER] = user['id']
            enrollment[C.ENROLLMENT_TABLE_PASSWORD] = generate_password()
            enrollment[C.ENROLLMENT_TABLE_IS_ENROLLED] = False
            user['passcode'] = enrollment[C.ENROLLMENT_TABLE_PASSWORD]
            print 'adding enrollment ', enrollment
            user['enrollment_id'] = enrollment_helper.add_enrollment(
                enrollment)
            user['company_name'] = company_name
        if callback is not None:
            callback(users)
        return users
Beispiel #16
0
    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'
Beispiel #17
0
    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))
Beispiel #18
0
    def run(self):
        # Return All the users in the User table
        log = Logger('RolesGetHandler')
        tag = 'run'

        final_dict = {}
        role = RoleDBHelper()
        team = TeamDBHelper()
        user = UserDBHelper()

        role_name = self.request.get_argument('role', None)
        offset = self.request.get_argument('offset', None)
        count = self.request.get_argument('count', None)
        name_query = self.request.get_argument('name', None)
        team_query = self.request.get_argument('team', None)
        device_query = self.request.get_argument('device_id', None)
        sort_by = self.request.get_argument(
            'sort_by',
            True)  # Intentionally done
        sort_order = self.request.get_argument('sort', None)
        filter_key = str(self.request.get_argument('filter_key', None))
        filter_value = self.request.get_argument('filter_value', None)

        print role_name
        print "\nUI sorting order \n", sort_order
        # Find all the roles in the Roles Table
#        outer_array = []

        if name_query:
            query = name_query
            query_type = 'name'
        elif team_query:
            query = team_query
            query_type = 'team'
        elif device_query:
            query = device_query
            query_type = 'device'
        else:
            query = None
            query_type = None

        if filter_key == 'team':
            team_name = filter_value
            team_id = team.get_team_by_name(str(team_name), self.company_id)
        else:
            team_name = None
            team_id = None

        print "printing team id here ..", team_id
        if role_name:
            role_id = role.get_role_by_name(str(role_name), self.company_id)
        else:
            roles_list = role.get_roles(self.company_id, [c.ROLE_TABLE_NAME,
                                                          c.ROLE_TABLE_ID])
            role_id = None

        if role_id:
            result_list, total_count = user.get_users_for_role(
                role_name=role_name, role_id=role_id,
                team_name=team_name, team_id=team_id, offset=offset,
                count=count, sort_by=sort_by, query=query,
                query_type=query_type, sort_order=sort_order)

        elif roles_list:
            for _role in roles_list:
                _role['role_type'] = _role.get('name')
                _role['role_name'] = _role.get('name')
                _role['role_id'] = _role.get('id')
                del _role['name']
                del _role['id']
            result_list = roles_list
            total_count = len(result_list)
        else:
            result_list = None

        if result_list:
            final_dict['data'] = result_list
            final_dict['count'] = total_count
            final_dict['pass'] = True
            final_dict['message'] = 'Seems like things are working ...'
        else:
            final_dict['data'] = []
            final_dict['pass'] = True
            log.e(tag, 'No Role in Role Table')
            final_dict['message'] = 'Seems like things are not working ...'
            final_dict['count'] = 0

        opJson = json.dumps(final_dict)
        self.request.write(opJson)
        tornado.ioloop.IOLoop.instance().add_callback(self.callback)
Beispiel #19
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)
Beispiel #20
0
    def run(self):
        #Return All the users in the User table
        log = Logger('RolesGetHandler')
        tag = 'run'

        final_dict = {}
        role = RoleDBHelper()
        team = TeamDBHelper()
        user = UserDBHelper()

        role_name = self.request.get_argument('role', None)
        offset = self.request.get_argument('offset', None)
        count = self.request.get_argument('count', None)
        name_query =  self.request.get_argument('name', None)
        team_query = self.request.get_argument('team', None)
        device_query = self.request.get_argument('device_id', None)
        sort_by = self.request.get_argument('sort_by', True)#Intentionally done
        sort_order = self.request.get_argument('sort', None)
        filter_key = str(self.request.get_argument('filter_key', None))
        filter_value = self.request.get_argument('filter_value', None)

        print role_name
        print "\nUI sorting order \n", sort_order
        ## Find all the roles in the Roles Table
#        outer_array = []

        if name_query:
            query  = name_query
            query_type = 'name'
        elif team_query:
            query = team_query
            query_type = 'team'
        elif device_query:
            query = device_query
            query_type = 'device'
        else:
            query = None
            query_type = None

        if filter_key == 'team':
            team_name = filter_value
            team_id = team.get_team_by_name(str(team_name), self.company_id)
        else:
            team_name = None
            team_id=None

        print "printing team id here ..", team_id
        if role_name:
            role_id = role.get_role_by_name(str(role_name), self.company_id)
        else:
            roles_list = role.get_roles(self.company_id, [c.ROLE_TABLE_NAME,
                                                        c.ROLE_TABLE_ID])
            role_id = None

        if role_id:
            result_list, total_count = user.get_users_for_role(
                        role_name=role_name, role_id=role_id,
                        team_name=team_name, team_id=team_id, offset=offset,
                        count=count, sort_by=sort_by, query=query,
                        query_type=query_type, sort_order=sort_order)

        elif roles_list:
            for _role in roles_list:
                _role['role_type'] = _role.get('name')
                _role['role_name'] = _role.get('name')
                _role['role_id'] = _role.get('id')
                del _role['name']
                del _role['id']
            result_list = roles_list
            total_count = len(result_list)
        else:
            result_list = None

        if result_list:
            final_dict['data'] = result_list
            final_dict['count'] = total_count
            final_dict['pass'] = True
            final_dict['message'] = 'Seems like things are working ...'
        else:
            final_dict['data'] = []
            final_dict['pass'] = True
            log.e(tag,'No Role in Role Table')
            final_dict['message'] = 'Seems like things are not working ...'
            final_dict['count'] = 0


        opJson = json.dumps(final_dict)
        self.request.write(opJson)
        tornado.ioloop.IOLoop.instance().add_callback(self.callback)
Beispiel #21
0
    def run(self):
        # Return All the users in the User table
        log = Logger('TeamsGetHandlerThread')
        tag = 'run'
        print 'In TeamsGetHandlerThread'
        final_dict = {}

        role = RoleDBHelper()
        team = TeamDBHelper()
        user = UserDBHelper()

        team_name = self.request.get_argument('team', None)
        offset = self.request.get_argument('offset', None)
        count = self.request.get_argument('count', None)
        sort_by = self.request.get_argument('sort_by',
                                            True)  # Intentionally done
        name_query = self.request.get_argument('name', None)
        role_query = self.request.get_argument('role', None)
        device_query = self.request.get_argument('device_id', None)
        sort_order = self.request.get_argument('sort', None)
        filter_key = str(self.request.get_argument('filter_key', None))
        filter_value = self.request.get_argument('filter_value', None)

        # Find all the roles in the Roles Table

        if filter_key == 'role':
            role_name = filter_value
            role_id = role.get_role_by_name(str(role_name), self.company_id)
        else:
            role_name = None
            role_id = None

        if name_query:
            query = name_query
            query_type = 'name'
        elif role_query:
            query = role_query
            query_type = 'role'
        elif device_query:
            query = device_query
            query_type = 'device'
        else:
            query = None
            query_type = None

        if team_name:
            team_id = team.get_team_by_name(str(team_name), self.company_id)
        else:
            team_id = None
            teams_list = team.get_teams(self.company_id,
                                        [c.TEAM_TABLE_NAME, c.TEAM_TABLE_ID])

        if team_id:
            result_list, total_count = user.get_users_for_team(
                team_name=team_name,
                team_id=team_id,
                role_name=role_name,
                role_id=role_id,
                offset=offset,
                count=count,
                sort_by=sort_by,
                query=query,
                query_type=query_type,
                sort_order=sort_order)

        elif teams_list:
            for _team in teams_list:
                _team['team_type'] = _team.get('name')
                _team['team_name'] = _team.get('name')
                _team['team_id'] = _team.get('id')
                del _team['name']
                del _team['id']
            result_list = teams_list
            total_count = len(result_list)

        else:
            result_list = None

        if result_list:
            print "\n total", total_count
            final_dict['data'] = result_list
            final_dict['count'] = total_count
            final_dict['pass'] = True
            final_dict['message'] = 'Success ...'
        else:
            final_dict['pass'] = True
            final_dict['data'] = []
            log.e(tag, 'No Team in Team Table')
            final_dict['message'] = 'Failed ...'
            final_dict['count'] = 0

        opJson = json.dumps(final_dict)
        self.request.write(opJson)
        tornado.ioloop.IOLoop.instance().add_callback(self.callback)
Beispiel #22
0
    def run(self):
        log = Logger('RolesPutHandlerThread')
        TAG = 'PUT'

        input_dict = json.loads(self.request.request.body)

        self.role_id = input_dict.get('role_id')
        if self.role_id is None:
            log.e(TAG, 'No role_id sent aborting update')
            opJson = json.dumps({'pass': False, 'message': 'No role_id sent'})
            self.request.set_status(401)
            self.request.write(opJson)
            tornado.ioloop.IOLoop.instance().add_callback(self.callback)

        else:
            if 'name' in input_dict:
                print 'Name found'
                self.role_name = input_dict['name']
                if self.role_name is None:
                    log.e(TAG, 'No role_name sent aborting update')
                    opJson = json.dumps(
                        {'pass': False, 'message': 'No role_name sent'})
                    self.request.write(opJson)
                    tornado.ioloop.IOLoop.instance().add_callback(
                        self.callback)
                else:
                    role = RoleDBHelper()
                    update_dict = {
                        c.ROLE_TABLE_NAME: self.role_name
                    }
                    result = role.update_role(str(self.role_id),
                                              self.company_id, update_dict)
                    if not result:
                        log.e(TAG, 'Not able to update the role_name at DB')
                        opJson = json.dumps(
                            {'pass': False,
                                'message': 'Not able to update the \
role_name at DB'})
                        self.request.write(opJson)
                        tornado.ioloop.IOLoop.instance().add_callback(
                            self.callback)
                    else:
                        log.i(TAG, 'role_name Updated successfully')
                        opJson = json.dumps(
                            {'pass': True,
                                'message': 'role_name updated for role.'})
                        self.request.write(opJson)
                        tornado.ioloop.IOLoop.instance().add_callback(
                            self.callback)

            elif 'policy_id' in input_dict:
                print 'Policy found'
                self.role_policy_id = input_dict['policy_id']
                if self.role_policy_id is None:
                    log.e(TAG, 'No Policy ID sent aborting update')
                    opJson = json.dumps(
                        {'pass': False, 'message': 'No policy_id sent'})
                    self.request.write(opJson)
                    tornado.ioloop.IOLoop.instance().add_callback(
                        self.callback)
                else:
                    role = RoleDBHelper()
                    update_dict = {
                        c.ROLE_TABLE_POLICY: self.role_policy_id
                    }
                    result = role.update_role(str(self.role_id),
                                              self.company_id, update_dict)
                    if not result:
                        log.e(TAG, 'Not able to update the policy_id at DB')
                        opJson = json.dumps(
                            {'pass': False,
                                'message': 'Not able to update the \
policy_id at DB'})
                        self.request.write(opJson)
                        tornado.ioloop.IOLoop.instance().add_callback(
                            self.callback)
                    else:
                        log.i(TAG, 'policy_id Updated successfully')
                        opJson = json.dumps(
                            {'pass': True,
                                'message': 'policy_id updated for role.'})
                        self.request.write(opJson)
                        tornado.ioloop.IOLoop.instance().add_callback(
                            self.callback)
Beispiel #23
0
    def parse(self, excel_path, company, company_name=None, callback=None,
              user_added_callback=None):
        COMPANY = company
        company_name = company_name
        book = open_workbook(filename=excel_path)
        sheet = book.sheet_by_index(0)

        users = list()
        for row_index in range(sheet.nrows):
            user = dict()
            for col_index in range(sheet.ncols):
                cell = sheet.cell(row_index, col_index).value
                if col_index == 0:
                    user[C.USER_TABLE_EMAIL] = cell
                elif col_index == 1:
                    user[C.USER_TABLE_NAME] = cell
                elif col_index == 2:
                    user[C.USER_TABLE_TEAM] = cell
                elif col_index == 3:
                    user[C.USER_TABLE_ROLE] = cell
            users.append(user)
        roles_list = list()
        teams_list = list()
        for user in users:
            if C.USER_TABLE_ROLE in user:
                roles_list.append(user[C.USER_TABLE_ROLE])
            if C.USER_TABLE_TEAM in user:
                teams_list.append(user[C.USER_TABLE_TEAM])

        # get a set to get the unique elements.
        roles = dict()
        teams = dict()

        roles_helper = RoleDBHelper()
        teams_helper = TeamDBHelper()
        users_helper = UserDBHelper()
        enrollment_helper = EnrollmentDBHelper()

        for role_name in set(roles_list):
            role = dict()
            role[C.ROLE_TABLE_COMPANY] = COMPANY
            role[C.ROLE_TABLE_NAME] = role_name
            roles[role_name] = roles_helper.add_role(role)

        for team_name in set(teams_list):
            team = dict()
            team[C.TEAM_TABLE_COMPANY] = COMPANY
            team[C.TEAM_TABLE_NAME] = team_name
            team[C.TEAM_TABLE_DELETED] = False
            teams[team_name] = teams_helper.add_team(team)

        # Now we have id for all teams and roles... Insert the users now.
        for user in users:
            user_obj = dict()
            user_obj[C.USER_TABLE_COMPANY] = COMPANY
            user_obj[C.USER_TABLE_EMAIL] = user[C.USER_TABLE_EMAIL]
            user_obj[C.USER_TABLE_NAME] = user[C.USER_TABLE_NAME]
            user_obj[C.USER_TABLE_ROLE] = roles[user[C.USER_TABLE_ROLE]]
            user_obj[C.USER_TABLE_TEAM] = teams[user[C.USER_TABLE_TEAM]]
            user['id'] = users_helper.add_user_if_not_exists(user_obj)[0]
            if user_added_callback is not None:
                user_added_callback(user_obj)

        for user in users:
            enrollment = dict()
            enrollment[C.ENROLLMENT_TABLE_USER] = user['id']
            enrollment[C.ENROLLMENT_TABLE_PASSWORD] = generate_password()
            enrollment[C.ENROLLMENT_TABLE_IS_ENROLLED] = False
            user['passcode'] = enrollment[C.ENROLLMENT_TABLE_PASSWORD]
            print 'adding enrollment ', enrollment
            user['enrollment_id'] = enrollment_helper.add_enrollment(
                enrollment)
            user['company_name'] = company_name
        if callback is not None:
            callback(users)
        return users
Beispiel #24
0
    def run(self):
        # Return All the users in the User table
        log = Logger('UsersGetHandlerThreadWithPage')
        tag = 'run'

        company_id = self.company_id

        final_dict = {}

        user = UserDBHelper()
        team = TeamDBHelper()
        role = RoleDBHelper()
        violation = ViolationsDBHelper()
        device = DeviceDBHelper()

        offset = self.request.get_argument('offset', None)
        count = self.request.get_argument('count', None)
        name_query = self.request.get_argument('name', None)
        team_query = self.request.get_argument('team', None)
        role_query = self.request.get_argument('role', None)
        device_query = self.request.get_argument('device_id', None)
        sort_by = self.request.get_argument('sort_by',
                                            True)  # Intentionally done
        sort_order = self.request.get_argument('sort', None)
        filter_key = self.request.get_argument('filter_key', None)
        filter_value = self.request.get_argument('filter_value', None)

        if filter_key == 'role':
            role_name = filter_value
            role_id = role.get_role_by_name(str(role_name), company_id)
        else:
            role_name = None
            role_id = None

        if filter_key == 'team':
            team_name = filter_value
            team_id = team.get_team_by_name(str(team_name), company_id)
        else:
            team_name = None
            team_id = None

        if filter_key == 'os':
            os_mapper = {'Android': 'samsung', 'iOS': 'ios'}
            os_type = os_mapper.get(str(filter_value))
        else:
            os_type = None

        if name_query:
            query = name_query
            query_type = 'name'
        elif role_query:
            query = role_query
            query_type = 'role'
        elif device_query:
            query = device_query
            query_type = 'device'
        elif team_query:
            query = team_query
            query_type = 'team'
        else:
            query = None
            query_type = None

        if offset:
            result_list, total_count = user.get_users_for_user(
                company_id=company_id,
                offset=offset,
                count=count,
                role_id=role_id,
                team_id=team_id,
                query=query,
                query_type=query_type,
                os_type=os_type,
                sort_by=sort_by,
                sort_order=sort_order,
                filter_key=filter_key,
                filter_value=filter_value)

        else:
            result_list, total_count = user.get_users(
                {c.USER_TABLE_COMPANY: company_id})

        if result_list:
            for user_dict in result_list:
                device_deleted = False
                device_info = device.get_device_with_udid(
                    user_dict.get('user_device'))
                if not device_info:
                    device_info = device.get_device_with_udid(
                        user_dict.get('user_device'), status=True)
                    device_deleted = True
                if device_info:
                    device_id = device_info[0].get(c.DEVICE_TABLE_ID)
                else:
                    device_id = None

                if device_deleted:
                    user_dict['user_device'] = None
                    user_dict['user_device_os'] = None

                if device_id:
                    print "printing \n device id", device_id
                    violation_count = violation.get_violation_count(
                        company_id=company_id, device_id=str(device_id))
                else:
                    violation_count = 0
                user_dict['user_violation'] = violation_count

            final_dict['data'] = result_list
            final_dict['count'] = total_count
            final_dict['pass'] = True
            final_dict['message'] = 'Seems like things are working ...'

        else:
            final_dict['pass'] = True
            log.e(tag, 'No User in User Table')
            final_dict['message'] = 'Seems like things are not working ...'
            final_dict['count'] = 0

        # add all the data into dictionary and create output json
        opJson = json.dumps(final_dict)
        self.request.write(opJson)
        tornado.ioloop.IOLoop.instance().add_callback(self.callback)