示例#1
0
def delete_function(oid):
    if oid is not None:
        UserFunctionCache.flush_all()
        context = DBContext()
        context.delete_byid("um_function", oid)
        # delete related auth
        delete_auth_byidentity(oid, "UmFunction")
示例#2
0
def delete_auth_byidentity(oid, entity):
    context = DBContext()
    context.execute_delete("um_auth",
                           "(sourceid = :oid and sourceentity = :entity) or "
                           "(grantid = :oid and grantentity = :entity)",
                           oid=oid,
                           entity=entity)
示例#3
0
def get_auth_bydetail(source_id, source_entity, grant_id, grant_entity):
    context = DBContext()
    return context.create_query(
        "um_auth", "sourceid=:sourceid and sourceentity=:sourceentity "
        "and grantid=:grantid and grantentity=:grantentity",
        sourceid=source_id,
        sourceentity=source_entity,
        grantid=grant_id,
        grantentity=grant_entity).first()
示例#4
0
def find_schedule_logs(**params):
    sql = ['1=1']
    cond = {}
    ctx = DBContext()
    if 'scheduleid' in params:
        sql.append('and scheduleid = :scheduleid')
        cond['scheduleid'] = params['scheduleid']
    if 'status' in params:
        sql.append('and status = :status')
        cond['status'] = num.safe_int(params['status'])
    return ctx.create_query('cm_schedule_log', ' '.join(sql), **cond)
示例#5
0
def find_function_user_auth(function_id):
    context = DBContext()
    sql_arr = [
        "select u.id, u.loginid, u.name, a.id authid from um_user u",
        "left join (", "  select id, grantid from um_auth",
        "  where sourceentity = :functionentity and sourceid = :functionid and grantentity = :userentity",
        ") a on u.id = a.grantid", "where u.loginid <> 'admin'"
    ]
    return context.create_sql_query("\n".join(sql_arr),
                                    functionentity="UmFunction",
                                    functionid=function_id,
                                    userentity="UmUser")
示例#6
0
def find_my_menu_db(user_id):
    context = DBContext()
    ret = []
    user = get_user_byid(user_id)
    if user is None:
        return ret
    if user["loginid"] == "admin":
        return find_menu()
    else:
        sql = [
            "select m.* from um_menu m", "right join (",
            "	select * from um_auth where sourceentity = 'UmMenu' and grantentity = 'UmUser' and grantid = :userid",
            ") a on m.id = a.sourceid"
        ]
        return context.create_sql_query("\n".join(sql), userid=user_id)
示例#7
0
文件: task.py 项目: major1201/raphael
def task_execution_listener(e):
    from apscheduler.events import JobExecutionEvent
    from raphael.app.modules.schedule import models as schedule
    from raphael.utils.dao.context import DBContext

    assert isinstance(e, JobExecutionEvent)
    # add to task schedule history database
    if e.jobstore == schedule.TASK_DATABASE:
        status_dict = {
            events.EVENT_JOB_EXECUTED: 1,
            events.EVENT_JOB_ERROR: 2,
            events.EVENT_JOB_MISSED: 3,
            events.EVENT_JOB_MAX_INSTANCES: 4
        }
        _schedule = schedule.get_schedule(e.job_id)
        if _schedule is not None:
            with DBContext():
                if _schedule.get('type') == 1:
                    _schedule['enabled'] = 0
                    schedule.save_schedule(_schedule)
                schedule_log = {
                    'scheduleid': e.job_id,
                    'executiontime': e.scheduled_run_time,
                    'retval': None if e.retval is None else str(e.retval),
                    'status': status_dict[e.code],
                    'exception': str(e.exception)
                }
                schedule.save_schedule_log(schedule_log)
示例#8
0
def delete_schedule(oid):
    with DBContext() as ctx:
        ctx.delete_byid('cm_schedule', oid)
        ctx.execute_delete('cm_schedule_log', 'scheduleid = :sid', sid=oid)
        try:
            task.remove_job(oid, TASK_DATABASE)
        except JobLookupError:
            logger.error_traceback()
示例#9
0
 def actual_get(cls, key):
     sql = [
         "select f.name from um_function f", "right join (",
         "  select * from um_auth where sourceentity = 'UmFunction' and grantentity = 'UmUser' and grantid = :userid",
         ") a on f.id = a.sourceid"
     ]
     funcs = DBContext().create_sql_query('\n'.join(sql),
                                          userid=key).fetch()
     return [f.get('name') for f in funcs]
示例#10
0
def find_menu(**params):
    sql = ['1=1']
    cond = {}
    if 'parentid' in params:
        sql.append('parentid = :parentid')
        cond['parentid'] = params['parentid']
    if 'sort' in params:
        sql.append('sort = :sort')
        cond['sort'] = num.safe_int(params['sort'])
    return DBContext().create_query("um_menu", ' and '.join(sql), **cond)
示例#11
0
def find_schedules(**params):
    ctx = DBContext()
    sql = ['1=1']
    cond = {}
    if 'enabled' in params:
        sql.append('and enabled = :enabled')
        cond['enabled'] = params['enabled']
    if 'type' in params:
        sql.append('and type = :type')
        cond['type'] = params['type']
    if 'module' in params:
        sql.append('and module = :module')
        cond['module'] = params['module']
    if 'modulelike' in params:
        sql.append('and module like :modulelike')
        cond['modulelike'] = '%' + params['modulelike'] + '%'
    if 'sourceid' in params:
        sql.append('and sourceid = :sourceid')
        cond['sourceid'] = params['sourceid']
    return ctx.create_query('cm_schedule', ' '.join(sql), **cond)
示例#12
0
def add_umsession(user_id, duration):
    import datetime

    token = strings.uuid()
    DBContext().save(
        'um_session', {
            'token': token,
            'user_id': user_id,
            'expire_at': time.utcnow() + datetime.timedelta(seconds=duration),
        })
    return token
示例#13
0
def find_auth(**params):
    context = DBContext()
    where = ['1=1']
    argdict = {}

    for attribute in 'sourceentity', 'sourceid', 'grantentity', 'grantid':
        try:
            value = params[attribute]
            where.append('%s=:%s' % (attribute, attribute))
            argdict[attribute] = value
        except KeyError:
            pass
    for a in 'sourceentityin', 'sourceidin', 'grantentityin', 'grantidin':
        try:
            attribute = a[:-2]
            values = params[a]
            if not isinstance(values, collections.Iterable):
                raise Exception('%s should be iterable', a)
            where.append('%s in %s' %
                         (attribute, query.escape_sequence(values)))
        except KeyError:
            pass
    return context.create_query('um_auth', ' and '.join(where), **argdict)
示例#14
0
def delete_menu(oid):
    menu = get_menu(oid)
    if menu is not None:
        with DBContext() as ctx:
            ctx.delete_byid("um_menu", oid)
            # revise sort
            ctx.execute(
                'update um_menu set sort = sort - 1 where parentid = :parentid and sort > :sort',
                parentid=menu['parentid'],
                sort=menu['sort'])
            # remove children
            ctx.execute_delete('um_menu',
                               'parentid = :parentid',
                               parentid=menu['id'])
            # delete related auth
            delete_auth_byidentity(oid, "UmMenu")
            # clear my menu cache
            UserMenuCache.flush_all()
示例#15
0
def find_settings(**params):
    where = ['1=1']
    cond = {}
    if 'name' in params:
        where.append('name = :name')
        cond['name'] = params['name']
    if 'namelike' in params:
        where.append('name like :namelike')
        cond['namelike'] = '%' + params['namelike'] + '%'
    if 'namelikeleft' in params:
        where.append('name like :namelikeleft')
        cond['namelikeleft'] = params['namelikeleft'] + '%'
    if 'value' in params:
        where.append('value = :value')
        cond['value'] = params['value']
    if 'notid' in params:
        where.append('id != :notid')
        cond['notid'] = params['notid']
    return DBContext().create_query(CM_SETTING, ' and '.join(where), **cond)
示例#16
0
def move_menu(menu, operator):
    with DBContext() as ctx:
        changed = False
        if operator == 'top':
            if menu['sort'] != 1:
                ctx.execute(
                    'update um_menu set sort = sort + 1 where parentid = :parentid and sort < :sort',
                    parentid=menu['parentid'],
                    sort=menu['sort'])
                menu['sort'] = 1
                changed = True
        elif operator == 'up':
            if menu['sort'] != 1:
                ctx.execute(
                    'update um_menu set sort = sort + 1 where parentid = :parentid and sort = :sort',
                    parentid=menu['parentid'],
                    sort=menu['sort'] - 1)
                menu['sort'] = menu['sort'] - 1
                changed = True
        elif operator == 'down':
            if menu['sort'] != find_menu(parentid=menu['parentid']).count():
                ctx.execute(
                    'update um_menu set sort = sort - 1 where parentid = :parentid and sort = :sort',
                    parentid=menu['parentid'],
                    sort=menu['sort'] + 1)
                menu['sort'] = menu['sort'] + 1
                changed = True
        elif operator == 'bottom':
            if menu['sort'] != find_menu(parentid=menu['parentid']).count():
                ctx.execute(
                    'update um_menu set sort = sort - 1 where parentid = :parentid and sort > :sort',
                    parentid=menu['parentid'],
                    sort=menu['sort'])
                menu['sort'] = find_menu(parentid=menu['parentid']).count()
                changed = True
        if changed:
            ctx.save('um_menu', menu)
            UserMenuCache.flush_all()
示例#17
0
def find_umsessions(**params):
    sql_arr = ['1=1']
    cond = {}

    # token
    try:
        v = params['token']
        sql_arr.append('token = :token')
        cond['token'] = v
    except KeyError:
        pass

    # user_id
    try:
        v = params['user_id']
        sql_arr.append('user_id = :user_id')
        cond['user_id'] = v
    except KeyError:
        pass

    # expire_at
    try:
        v = params['gt_expire_at']
        sql_arr.append('expire_at > :gt_expire_at')
        cond['gt_expire_at'] = v
    except KeyError:
        pass
    try:
        v = params['lt_expire_at']
        sql_arr.append('expire_at > :lt_expire_at')
        cond['lt_expire_at'] = v
    except KeyError:
        pass

    return DBContext().create_query('um_session', ' and '.join(sql_arr),
                                    **cond)
示例#18
0
def save_schedule_manually(o):
    with DBContext():
        save_schedule(o)
        save_task_schedule(o)
示例#19
0
def save_schedule(o):
    with DBContext() as ctx:
        ctx.save('cm_schedule', o)
示例#20
0
def save_schedule_log(o):
    ctx = DBContext()
    ctx.save('cm_schedule_log', o)
示例#21
0
def get_schedule(oid):
    ctx = DBContext()
    return ctx.get('cm_schedule', oid)
示例#22
0
 def actual_save(cls, key, obj):
     DBContext().save(cls.prefix, obj)
示例#23
0
 def actual_remove(cls, key):
     DBContext().execute_delete(cls.prefix, cls.index_key + '=:indexkey', indexkey=key)
示例#24
0
def save_auth(auth):
    assert isinstance(auth, dict)
    context = DBContext()
    context.save("um_auth", auth)
示例#25
0
def get_setting(oid):
    return DBContext().get(CM_SETTING, oid)
示例#26
0
def find_users():
    return DBContext().create_query("um_user", "1=1")
示例#27
0
def scrub_expired_umsessions():
    DBContext().execute_delete('um_session',
                               'expire_at < :now',
                               now=time.utcnow())
示例#28
0
def get_umsession(oid):
    return DBContext().get('um_session', oid)
示例#29
0
 def actual_get(cls, key):
     if cls.index_key == PRIMARY_IDENTIFIER:
         return DBContext().get(cls.prefix, key)
     else:
         return DBContext().create_query(cls.prefix, cls.index_key + '=:' + cls.index_key, **{cls.index_key: key}).first()
示例#30
0
def delete_auth(oid):
    if oid is not None:
        context = DBContext()
        context.delete_byid("um_auth", oid)