def update_role(self, role_id, role_name, note_info):
     """
     修改角色
     :param role_id: 角色ID
     :param role_name: 角色名称
     :param note_info: 备注信息
     :return:
     """
     db_conn = MysqlConn()
     try:
         time_modify = get_system_datetime()
         db_name = configuration.get_database_name()
         table_name = 'role'
         filed = 'name'
         condition = "id <> %s and name = '%s'" % (role_id, role_name)
         old_sql = self.create_select_sql(db_name, table_name, filed,
                                          condition)
         old_pla_name_data = self.execute_fetch_one(db_conn, old_sql)
         if old_pla_name_data:
             return response_code.RECORD_EXIST
         else:
             fields = ['id', 'name', 'note_info', 'time_modify']
             pla_data = [role_id, role_name, note_info, time_modify]
             update_condition = 'id = %s' % str(role_id)
             update_sql = self.create_update_sql(db_name, table_name,
                                                 fields, pla_data,
                                                 update_condition)
             self.execute_update_sql(db_conn, update_sql)
             return response_code.SUCCESS
     except Exception as e:
         lg.error(e)
         return response_code.UPDATE_DATA_FAIL
     finally:
         db_conn.close()
 def add_role(self, role_name, note_info):
     """
     添加角色
     :param role_name: 角色名称
     :param note_info: 备注信息
     :return:
     """
     db_conn = MysqlConn()
     try:
         create_time = get_system_datetime()
         db_name = configuration.get_database_name()
         table_name = 'role'
         filed = 'name'
         condition = "name = '%s'" % role_name
         system_name_sql = self.create_select_sql(db_name, table_name,
                                                  filed, condition)
         res = self.execute_fetch_one(db_conn, system_name_sql)
         if res:
             return response_code.RECORD_EXIST
         else:
             fields = '(name, create_time,note_info)'
             insert_data = (role_name, create_time, note_info)
             sql = self.create_insert_sql(db_name, table_name, fields,
                                          insert_data)
             self.execute_sql_return_count(db_conn, sql)
             return response_code.SUCCESS
     except Exception as e:
         lg.error(e)
         error_data = response_code.ADD_DATA_FAIL
         return error_data
     finally:
         db_conn.close()
Beispiel #3
0
    def add_user(self, user):
        """
        添加用户
        :return:
        """
        conn = MysqlConn()
        try:
            db_name = configuration.get_database_name()
            # 解析参数成dict

            # 判断用户是否已经存在
            if_user = self.get_user(user_name=user['user_name'])
            if if_user:
                data = response_code.RECORD_EXIST
                return data
            # 需要插入的字段
            fields = '(user_name,user_sex,email,pass_word,phone,position,create_time,note_info,login_name,icon)'
            # 获取当前创建时间
            create_time = get_system_datetime()
            pass_word = generate_password_hash('123456')
            # 插入值
            value_tuple = (user['user_name'], user['user_sex'], user['email'],
                           pass_word, user['phone'], user['position'],
                           create_time, user['note_info'], user['login_name'],
                           user['icon'])
            # 构造插入用户的sql语句
            insert_user_sql = self.create_insert_sql(db_name, 'users', fields,
                                                     value_tuple)
            self.insert_exec(conn, insert_user_sql)
            # 查询插入新用户
            new_user = self.get_user(user_name=user['user_name'])
            # 构造插入部门关联表的sql语句,并插入值
            for dpt_id in eval(str(user['dpt_ids'])):
                insert_users_department_sql = self.create_insert_sql(
                    db_name, 'users_department', '(user_id,dpt_id)',
                    (new_user['ID'], dpt_id))
                self.insert_exec(conn, insert_users_department_sql)
            # 构造插入用户组关联表的sql语句,并插入值
            for group_id in eval(str(user['group_ids'])):
                insert_users_group_sql = self.create_insert_sql(
                    db_name, 'user_user_group', '(user_id,group_id)',
                    (new_user['ID'], group_id))
                self.insert_exec(conn, insert_users_group_sql)
            # 构造插入用户角色关联表的sql语句,并插入值
            for role_id in eval(str(user['role_ids'])):
                insert_users_role_sql = self.create_insert_sql(
                    db_name, 'user_roles', '(user_id,role_id)',
                    (new_user['ID'], role_id))
                self.insert_exec(conn, insert_users_role_sql)
            data = response_code.SUCCESS
            return data
        except Exception as e:
            lg.error(e)
            conn.conn.rollback()
            return response_code.ADD_DATA_FAIL
        finally:
            conn.close()
Beispiel #4
0
 def add_operation_log(self, user_key, ip_address, level, description):
     """
     添加操作日志
     :param user_key:
     :param ip_address:
     :param level:
     :param description:
     :return:
     """
     db_conn = MysqlConn()
     time_create = get_system_datetime()
     db_name = configuration.get_database_name()
     table_name = 'system_oper_log'
     fields = '(user_key,ip_address,level,description,time_create)'
     pla_data = (user_key, ip_address, level, description, time_create)
     sql = DbBase.create_insert_sql(self, db_name, table_name, fields,
                                    pla_data)
     DbBase.execute_sql_return_count(self, db_conn, sql)
     db_conn.close()
Beispiel #5
0
 def add_group(self, group_name, group_desc, role_ids):
     """
     添加用户组
     :param group_name:
     :param group_desc:
     :return:
     """
     db_conn = MysqlConn()
     try:
         db_name = configuration.get_database_name()
         table_name = 'user_group'
         filed = 'name'
         condition = "name = '%s'" % group_name
         is_exist_sql = self.create_select_sql(db_name, table_name, filed,
                                               condition)
         res = self.execute_fetch_one(db_conn, is_exist_sql)
         if res:
             return response_code.RECORD_EXIST
         fields = '(name,description,create_time)'
         create_time = get_system_datetime()
         value_tuple = (group_name, group_desc, create_time)
         insert_user_sql = self.create_insert_sql(db_name, table_name,
                                                  fields, value_tuple)
         self.insert_exec(db_conn, insert_user_sql)
         # 查询新添加的用户组
         new_group = self.get_user_group_by_name(group_name)
         new_group_id = new_group.get('id')
         # 添加用户组的角色
         group_role_fields = '(group_id,role_id)'
         for role_id in eval(str(role_ids)):
             group_role_value_tuple = (new_group_id, role_id)
             insert_user_sql = self.create_insert_sql(
                 db_name, "user_group_role", group_role_fields,
                 group_role_value_tuple)
             self.insert_exec(db_conn, insert_user_sql)
         return response_code.SUCCESS
     except Exception as e:
         lg.error(e)
         return response_code.ADD_DATA_FAIL
     finally:
         db_conn.close()
Beispiel #6
0
 def update_system_log(self, log_key, opinion, opinion_user, status):
     """
     处理后,修改日志信息
     :param log_key:日志id
     :param opinion:处理意见
     :param opinion_user:处理人
     :param status:状态
     :return:
     """
     db_conn = MysqlConn()
     try:
         db_name = configuration.get_database_name()
         table_name = 'system_log'
         condition = "log_key=%s" % log_key
         # 查看状态是否已经更新
         sql = self.create_select_sql(db_name, table_name, 'status',
                                      condition)
         log = self.execute_fetch_one(db_conn, sql)
         if log.get('status') == 2:
             return response_code.ALREADY_HANDLED
         # 更新日志信息
         opinion_time = get_system_datetime()
         update_group_fields = [
             'opinion', 'opinion_user', 'status', 'opinion_time'
         ]
         update_group_fields_value = [
             opinion, opinion_user, status, opinion_time
         ]
         update_group_sql = self.create_update_sql(
             db_name, table_name, update_group_fields,
             update_group_fields_value, condition)
         self.updete_exec(db_conn, update_group_sql)
         return response_code.SUCCESS
     except Exception as e:
         lg.error(e)
         return response_code.UPDATE_DATA_FAIL
     finally:
         db_conn.close()