def user():
    """
        update user info
    :return: respond model
    """
    request_model = RequestModel(request)
    user_info_form = request_model.data.get('user_info')
    jwt = request_model.token
    user_info_jwt = decode_jwt(jwt)['user_info']
    respond_model = RespondModel()
    save_user_info = False
    if (user_info_form and user_info_form['name']
            == user_info_jwt['name']) or 'admin' in user_info_jwt['roles']:
        if not user_info_form.get('password') or user_info_form.get(
                'password') == '':
            user_info_form['password'] = get_password_from_db(user_info_form)
            save_user_info = user_service.update(user_info_form)
        else:
            save_user_info = update_password(user_info_form)
        respond_model.message = 'success'
        if save_user_info:
            respond_model.token = generate_jwt(user_info_form)
        return respond_model
    respond_model.message = 'error'
    return respond_model
def login():
    """
        user login
    :return: respond model with jwt token in headers
    """
    request_model = RequestModel(request)
    user_info_db = user_service.login(request_model.data.get('user_info'))
    respond_model = RespondModel()
    if user_info_db is not None:
        respond_model.token = generate_jwt(user_info_db)
        respond_model.message = 'login success'
        respond_model.code = 20000
    else:
        respond_model.message = 'username or password wrong!'
    return respond_model.dump_json()
def user_logout():
    """
        logout
    :return: respond model
    """
    respond_model = RespondModel()
    respond_model.message = 'success'
    respond_model.token = ''
    respond_model.code = 20000
    return respond_model.dump_json(), 200
Esempio n. 4
0
 def fun_dec(*args, **kwargs):
     request_model = RequestModel(request)
     user_info = decode_jwt(request_model.token)['user_info']
     if self.roles in user_info.get('roles'):
         respond_model = api_function(*args, **kwargs)
         return respond_model
     else:
         respond_model = RespondModel()
         respond_model.message = 'authorization error'
         return respond_model
Esempio n. 5
0
def check_enable_sign_up():
    """
        check if the server enable user sign up
    : return: respond model with boolean
    """
    enable_sign_up = get_config(APP_CONFIG)['ENABLE_SIGN_UP']
    respond_model = RespondModel()
    respond_model.data = {"enable_sign_up": enable_sign_up}
    respond_model.message = 'success'
    respond_model.code = 20000
    return respond_model.dump_json(), 200
Esempio n. 6
0
def user_info():
    """
        get user info
    :return: respond model
    """
    request_model = RequestModel(request)
    jwt = request_model.token
    user_info_jwt = decode_jwt(jwt)['user_info']
    respond_model = RespondModel()
    respond_model.message = 'success'
    respond_model.data = user_info_jwt
    return respond_model
Esempio n. 7
0
def plugin_info(lang, plugin_name):
    """
        return plugin info if user have authority
    :param lang: language for i18n
    :param plugin_name: plugin name
    :return: respond_model
    """
    respond_model = RespondModel()
    if not plugin_authorization(plugin_name):
        respond_model.message = 'authorization error'
        return respond_model
    respond_model.data = get_plugin_info(plugin_name, lang)
    return respond_model
    def fun_dec(*args, **kwargs):
        request_model = RequestModel(request)
        if validate_master(request_model.data.get("token")):
            respond_model = api_function(*args, **kwargs)
            respond_model.code = 20000
            respond_model.message = 'success'
            return respond_model.dump_json(), 200

        else:
            respond_model = RespondModel()
            respond_model.message = 'authorization error'
            respond_model.code = 50012
            return respond_model.dump_json(), 403
def user_info():
    """
        get user info
    :return: respond model
    """
    request_model = RequestModel(request)
    jwt = request_model.token
    user_info_jwt = decode_jwt(jwt)['user_info']
    respond_model = RespondModel()
    respond_model.message = 'success'
    if 'admin' in user_info_jwt.get('roles'):
        user_info_jwt['roles'] = 'admin,' + get_all_plugin_name()
    respond_model.data = user_info_jwt
    return respond_model
def save_token():
    """
        save plugin token
    :return:
    """
    request_model = RequestModel(request)
    jwt = request_model.token
    user_info_jwt = decode_jwt(jwt)['user_info']
    respond_model = RespondModel()
    respond_model.message = 'success'
    token = generate_token(user_info_jwt)
    respond_model.data['token'] = token
    user_info_jwt['token'] = token
    respond_model.token = generate_jwt(user_info_jwt)
    return respond_model
Esempio n. 11
0
def save_setting(plugin_name):
    """
        save user plugin setting
    :param plugin_name: plugin name
    :return: respond_model
    """
    respond_model = RespondModel()
    request_model = RequestModel(request)
    jwt = request_model.token
    user_info_jwt = decode_jwt(jwt)['user_info']
    if not plugin_authorization(plugin_name):
        respond_model.message = 'authorization error'
        return respond_model
    save_plugin_setting(plugin_name, request_model.data, user_info_jwt)
    return respond_model
Esempio n. 12
0
 def fun_dec(*args, **kwargs):
     request_model = RequestModel(request)
     if request_model.token and verify_jwt(request_model.token):
         respond_model = api_function(*args, **kwargs)
         if not respond_model.token:
             respond_model.token = renew_jwt(request_model.token)
         if respond_model.message == 'authorization error':
             respond_model.code = 50012
             return respond_model.dump_json(), 403
         respond_model.code = 20000
         respond_model.message = 'success'
         return respond_model.dump_json(), 200
     else:
         respond_model = RespondModel()
         respond_model.message = 'authentication error, please login'
         respond_model.code = 50012
         return respond_model.dump_json(), 401
Esempio n. 13
0
def plugin_setting(lang, plugin_name):
    """
        get plugin setting form and user plugin setting
    :param lang: language for i18n
    :param plugin_name: plugin name
    :return: respond_model
    """
    respond_model = RespondModel()
    request_model = RequestModel(request)
    jwt = request_model.token
    user_info_jwt = decode_jwt(jwt)['user_info']
    if not plugin_authorization(plugin_name):
        respond_model.message = 'authorization error'
        return respond_model
    data = {
        'form': get_plugin_setting(plugin_name, lang),
        'userSetting': get_user_plugin_setting(plugin_name, user_info_jwt)
    }
    respond_model.data = data
    return respond_model