예제 #1
0
def follow_user():
    """
    关注一个用户
    :return:
    """

    ids = json_to_pyseq(request.argget.all("ids", []))

    s, r = arg_verify(reqargs=[("id", ids)], required=True)
    if not s:
        return r

    cnt = 0
    for tid in ids[:]:

        if tid != current_user.str_id and get_one_user(user_id=str(tid)):
            r = mdb_user.db.user_follow.update_one(
                {
                    "user_id": current_user.str_id,
                    "type": "account"
                }, {"$addToSet": {
                    "follow": tid
                }},
                upsert=True)
            if r.modified_count or r.upserted_id:
                cnt += 1
                # 更新粉丝统计
                update_one_user(user_id=str(tid),
                                updata={"$inc": {
                                    "fans_num": 1
                                }})
            delete_user_info_cache(user_id=tid)

    if cnt:
        # 更新关注统计
        update_one_user(user_id=current_user.str_id,
                        updata={"$inc": {
                            "follow_user_num": cnt
                        }})
        delete_user_info_cache(user_id=current_user.str_id)
        data = {
            "msg": gettext("Followed"),
            "msg_type": "s",
            "http_status": 201
        }

    elif len(ids) == 1 and ids[0] == current_user.str_id:
        data = {
            "msg": gettext("You can't follow yourself"),
            "msg_type": "w",
            "http_status": 400
        }
    else:
        data = {
            "msg": gettext("You are already following this user"),
            "msg_type": "w",
            "http_status": 400
        }

    return data
예제 #2
0
    def get_login_token(self, user):
        """
        用户登录成功后调用此函数,记录登录并获取token
        :param user:
        :return: json
        """
        now_time = time.time()
        result = self.encode_auth_token(user.str_id, now_time)

        # 查看当前jwt验证的登录客户端数
        user_jwt = mdbs["user"].db.user.find_one(
            {"_id": user.id, "jwt_login_time": {"$exists": True}}, {"jwt_login_time": 1})
        if user_jwt:
            jwt_login_time = user_jwt["jwt_login_time"]
            keys = jwt_login_time.keys()
            if len(keys) >= get_config(
                "rest_auth_token",
                    "MAX_SAME_TIME_LOGIN"):
                earliest = 0
                earliest_cid = None
                for k, v in jwt_login_time.items():
                    if v < earliest or earliest == 0:
                        earliest = v
                        earliest_cid = k
                if earliest_cid:
                    del jwt_login_time[earliest_cid]
        else:
            jwt_login_time = {}

        jwt_login_time[result["cid"]] = now_time
        update_one_user(user_id=user.id,
                        updata={"$set": {"jwt_login_time": jwt_login_time}})
        return result["token"].decode()
예제 #3
0
def unfollow():
    '''
    取消关注
    :return:
    '''

    ids = json_to_pyseq(request.argget.all("ids", []))

    s, r = arg_verify(reqargs=[("id", ids)], required=True)
    if not s:
        return r

    for id in ids[:]:
        if mdb_user.db.user_follow.find_one({"user_id":current_user.str_id, "type": "account", "follow":id}):
            # 更新粉丝统计
            update_one_user(user_id=str(id), updata={"$inc": {"fans_num": -1}})
        else:
            ids.remove(id)
        delete_user_info_cache(user_id=id)

    r = mdb_user.db.user_follow.update_one({"user_id":current_user.str_id, "type": "account"},
                                           {"$pullAll": {"follow": ids}})

    if r.modified_count:
        # 更新关注统计
        update_one_user(user_id=current_user.str_id,
                        updata={"$inc": {"follow_user_num": -len(ids)}})
        delete_user_info_cache(user_id=current_user.str_id)
        data = {"msg": gettext("Unfollow success"), "msg_type": "s", "http_status": 201}
    else:
        delete_user_info_cache(user_id=current_user.str_id)
        data = {"msg": gettext("Unfollow failed"), "msg_type": "w", "http_status": 400}

    return data
예제 #4
0
    def clean_login(self):
        """
        清理用户登录
        :return:
        """

        auth_token = request.headers.get('OSR-BearerToken')
        if auth_token:
            payload = self.decode_auth_token(auth_token)
            if not isinstance(payload, str):
                user = get_one_user(user_id=str(payload['data']['id']))
                if not user:
                    result = (
                        None,
                        gettext(
                            "User authentication failed, user does not exist"))
                else:
                    if payload['data']["cid"] in user["jwt_login_time"] and \
                            user["jwt_login_time"][payload['data']["cid"]] == \
                            payload['data']['login_time']:

                        # 清除退出当前客户端的登录时间信息
                        user = get_one_user(user_id=str(payload['data']['id']))

                        del user["jwt_login_time"][payload["data"]["cid"]]
                        update_one_user(user_id=payload["data"]["id"],
                                        updata={
                                            "$set": {
                                                "jwt_login_time":
                                                user["jwt_login_time"]
                                            }
                                        })

                        result = (True, "")
                    else:
                        result = (True, "")
            else:
                result = (None, payload)
        else:
            result = (
                None,
                gettext(
                    'No user authentication token provided "OSR-BearerToken"'))
        return result
예제 #5
0
def p_password_reset(old_pass, new_pass):
    """
    用户密码修改
    :param old_pass:
    :param new_pass:
    :return:
    """

    r, s = password_format_ver(new_pass)
    if not r:
        data = {"msg_type": "w", "msg": s, "http_status": 400}
        return data

    if current_user.verify_password(old_pass) or current_user.no_password:
        password_hash = generate_password_hash(new_pass)
        # 将jwt_login_time设为{}退出所有jwt登录的用户
        r = update_one_user(
            user_id=current_user.str_id,
            updata={"$set": {
                "password": password_hash,
                "jwt_login_time": {}
            }})
        if r.modified_count:
            oplog = {
                'op_type': 'set_password',
                'time': time.time(),
                'status': 's',
                'info': '',
                'ip': request.remote_addr
            }
            insert_op_log(oplog)
            data = {
                "msg_type": "s",
                "msg":
                gettext("Password change is successful, please login again"),
                "http_status": 201
            }
            logout_user()
            data['to_url'] = get_config("login_manager", "LOGIN_VIEW")

        else:
            data = {
                "msg_type": "w",
                "msg": gettext("Password change failed(unknown error)"),
                "http_status": 400
            }
        return data

    data = {
        "msg_type": "e",
        "http_status": 400,
        "msg": gettext("Now use the password mistake")
    }

    return data
예제 #6
0
def user_edit():
    """
    用户编辑
    :return:
    """
    tid = request.argget.all('id')
    role_id = request.argget.all('role_id')
    active = str_to_num(request.argget.all('active', 0))

    s, r = arg_verify(reqargs=[("id", tid), ("role_id", role_id)],
                      required=True)
    if not s:
        return r

    data = {
        'msg': gettext("Update success"),
        'msg_type': "s",
        "custom_status": 201
    }
    update_data = {
        'role_id': role_id,
        'active': active,
    }
    user = get_one_user(user_id=str(tid))
    if user:
        # 权限检查
        current_user_role = mdbs["user"].db.role.find_one(
            {"_id": ObjectId(current_user.role_id)})
        edit_user_role = mdbs["user"].db.role.find_one(
            {"_id": ObjectId(user["role_id"])})
        if edit_user_role \
                and get_num_digits(current_user_role["permissions"]) \
                <= get_num_digits(edit_user_role["permissions"]):
            # 没有权限修改
            data = {
                "msg_type": "w",
                "msg": gettext("No permission modification"),
                "custom_status": 401
            }
            return data

    r = update_one_user(user_id=str(tid), updata={"$set": update_data})
    if not r.modified_count:
        data = {
            'msg': gettext("No changes"),
            'msg_type': "w",
            "custom_status": 201
        }
    return data
예제 #7
0
파일: sys_tool.py 프로젝트: yonghu86/osroom
def add_user(mdb_user):
    """
        初始化root用户角色, 管理员, 管理员基本资料

        :return:
        """
    from werkzeug.security import generate_password_hash
    from apps.utils.validation.str_format import email_format_ver, password_format_ver
    from apps.modules.user.models.user import user_model

    print(' * [User] add')
    is_continue = False
    while True:
        username = input("Input username:"******"[\.\*#\?]+", username):
            print(
                "[Warning]: The name format is not correct,You can't use '.','*','#','?'\n")
        else:
            break

    while not is_continue:
        email = input("Input email:")
        s, r = email_format_ver(email)
        if not s:
            print("[Warning]: {}".format(r))
        else:
            break

    while not is_continue:
        password = getpass("Input password(Password at least 8 characters):")
        s, r = password_format_ver(password)
        if not s:
            print("[Warning]: {}\n".format(r))
        else:
            break
    try:
        mdb_user.db.create_collection("role")
        print(' * Created role collection')
    except BaseException:
        pass
    try:
        mdb_user.db.create_collection("user")
        print(' * Created user collection')
    except BaseException:
        pass

    # 初始化角色
    root_per = SUPER_PER
    role_root = mdb_user.db.role.find_one({"permissions": root_per})
    if not role_root:
        print(" * Create root role...")
        r = mdb_user.db.role.insert_one({"name": "Root",
                                         "default": 0,
                                         "permissions": root_per,
                                         "instructions": 'Root'})

        if r.inserted_id:
            print("Create root user role successfully")
        else:
            print("[Error] Failed to create superuser role")
            sys.exit(-1)

        root_id = r.inserted_id
    else:
        root_id = role_root['_id']

    password_hash = generate_password_hash(password)
    user = get_one_user_mfilter(username=username, email=email, op="or")
    if user:
        update_one_user(user_id=str(user["_id"]),
                        updata={"$set": {"password": password_hash,
                                         "role_id": str(root_id)}})
        print(" * This user already exists, updated password.")
    else:
        print(' * Create root user...')
        user = user_model(
            username=username,
            email=email,
            password=password,
            custom_domain=-1,
            role_id=str(root_id),
            active=True)
        r = insert_one_user(updata=user)
        if r.inserted_id:
            print(" * Create a root user role successfully")
        else:
            print(" * [Error] Failed to create a root user role")
            sys.exit(-1)

    # To create the average user role
    average_user = mdb_user.db.role.find_one({"permissions": 1})
    if not average_user:
        print(" * Create the average user role...")
        r = mdb_user.db.role.insert_one({
            "name": "User",
            "default": 1,
            "permissions": 1,
            "instructions": 'The average user',
        })
        if r.inserted_id:
            print(" * Create a generic user role successfully")
        else:
            print(" * Failed to create a generic user role")

    role = mdb_user.db.role.find_one({"_id": root_id})
    hidden_password = "******".format(password[0:2], password[6:])
    print('The basic information is as follows')
    print('Username: {}\nEmail: {}\nUser role: {}\nPassword: {}'.format(
        username, email, role["name"], hidden_password))
    print('End')
예제 #8
0
def profile_update():
    """
    用户信息更新
    :return:
    """
    gender = request.argget.all('gender', 'secret')
    birthday = request.argget.all('birthday')
    homepage = request.argget.all('homepage')
    address = json_to_pyseq(request.argget.all('address', {}))
    info = request.argget.all('info')
    if len(birthday) != 8:
        data = {
            'msg':
            gettext("The date of birth requires an 8-digit date,Such as '{}'").
            format(time_to_utcdate(tformat="%Y%m%d")),
            'msg_type':
            "e",
            "custom_status":
            400
        }
        return data
    birthday = int(birthday)
    s, r = arg_verify(reqargs=[(gettext("gender"), gender)],
                      only=["secret", "m", "f"])
    if not s:
        return r
    addr_keys = ['countries', 'provinces', 'city', 'district', 'detailed']
    for k, v in address.items():
        if not (k in addr_keys) or not isinstance(v, str):
            data = {
                'msg':
                gettext(
                    "Address format is not in conformity with the requirements"
                ),
                'msg_type':
                "e",
                "custom_status":
                400
            }
            return data
    if homepage:
        s, r = url_format_ver(homepage)
        if not s:
            return {"msg": r, "msg_type": "w", "custom_status": 403}

    r = content_attack_defense(info)
    if r["security"] < 100:
        data = {
            'msg': gettext("User profile information is illegal"),
            'msg_type': "e",
            "custom_status": 400
        }
        return data
    update_data = {
        'gender': gender,
        'homepage': homepage,
        'introduction': info,
        'birthday': birthday,
        'address': address
    }
    r = update_one_user(user_id=current_user.str_id,
                        updata={"$set": update_data})

    if r.modified_count:
        # 清理user信息数据缓存
        delete_user_info_cache(user_id=current_user.str_id)
        data = {
            'msg': gettext("Update succeed"),
            'msg_type': "s",
            "custom_status": 201
        }
    else:
        data = {
            'msg': gettext("No changes"),
            'msg_type': "w",
            "custom_status": 201
        }
    return data
예제 #9
0
def user_basic_edit():
    """
    用户基础设置编辑
    :return:
    """
    username = request.argget.all('username')
    custom_domain = request.argget.all('custom_domain', '')
    editor = request.argget.all('editor')
    # username
    s, r = arg_verify(reqargs=[(gettext("username"), username)], required=True)
    if not s:
        return r
    r, s = short_str_verifi(username, "username")
    if not r:
        data = {'msg': s, 'msg_type': "e", "custom_status": 422}
        return data

    update_data = {}
    # custom_domain
    if mdbs["user"].db.user.find_one({
            "_id": current_user.id,
            "custom_domain": -1
    }) and custom_domain.strip():
        r, s = ver_user_domainhacks(custom_domain)
        if r:
            update_data["custom_domain"] = custom_domain
        else:
            data = {'msg': s, 'msg_type': "e", "custom_status": 422}
            return data

    update_data["username"] = username
    # editor
    if editor and editor in ['rich_text', 'markdown']:
        update_data["editor"] = editor
    else:
        data = {
            'msg': gettext("The editor saves failure"),
            'msg_type': "e",
            "custom_status": 400
        }
        return data

    update_data["update_time"] = time.time()

    # 是否被使用
    if mdbs["user"].db.user.find_one({
            "_id": {
                "$ne": current_user.id
            },
            "username": username
    }):
        data = {
            'msg': gettext("Name has been used"),
            'msg_type': "w",
            "custom_status": 403
        }
    elif "custom_domain" in update_data.keys() \
            and mdbs["user"].db.user.find_one({"_id": {"$ne": current_user.id}, "custom_domain": custom_domain}):
        data = {
            'msg': gettext("Domain has been used"),
            'msg_type': "w",
            "custom_status": 403
        }
    elif "custom_domain" in update_data.keys(
    ) and mdbs["user"].db.user.find_one({
            "_id": current_user.id,
            "custom_domain": {
                "$ne": -1
            }
    }):
        data = {
            'msg': gettext("Personality custom domain cannot be modified"),
            'msg_type': "w",
            "custom_status": 400
        }
    else:
        r = update_one_user(user_id=current_user.str_id,
                            updata={"$set": update_data})
        if not r.modified_count:
            data = {
                'msg': gettext("No changes"),
                'msg_type': "w",
                "custom_status": 201
            }
        else:
            delete_user_info_cache(user_id=current_user.str_id)
            data = {
                'msg': gettext("Update success"),
                'msg_type': "s",
                "custom_status": 201
            }

    return data
예제 #10
0
def p_retrieve_password(email, code, password, password2):
    """
    密码重设
    :param account:
    :param code:
    :param password:
    :param password2:
    :return:
    """

    data = {}
    if not email:
        data = {
            'msg': gettext('Account does not exist'),
            'msg_type': 'e',
            "http_status": 404
        }
        return data

    s, r = email_format_ver(email=email)
    if s:
        user = get_one_user(email=email)
    else:
        data = {"msg": r, "msg_type": "e", "http_status": 403}
        return data

    if user:
        r = verify_code(code, email=user["email"])
    else:
        data = {
            'msg': gettext('Account does not exist'),
            'msg_type': 'e',
            "http_status": 404
        }
        return data

    if not r:
        data = {
            'msg': gettext('Email or SMS verification code error'),
            'msg_type': 'e',
            "http_status": 401
        }
    else:
        if user:
            r = password_format_ver(password)
            if not r:
                data = {"msg": r, "msg_type": "e", "http_status": "403"}
                return data
            elif password != password2:
                data = {
                    'msg': gettext('Two password is not the same'),
                    'msg_type': 'w',
                    "http_status": 400
                }
            else:
                password_hash = generate_password_hash(password)
                # 将jwt_login_time设为{}退出所有jwt登录的用户
                r = update_one_user(user_id=str(user["_id"]),
                                    updata={
                                        "$set": {
                                            "password": password_hash,
                                            "jwt_login_time": {}
                                        }
                                    })

                if r.modified_count:
                    oplog = {
                        'op_type': 'retrieve_pass',
                        'time': time.time(),
                        'status': 's',
                        'info': '',
                        'ip': request.remote_addr
                    }
                    insert_op_log(oplog, user_id=user["_id"])

                    # 发送邮件
                    subject = gettext("Password reset notification")
                    body = "Your account <a>{}</a> has reset your password. <br>Please keep it safe.".format(
                        user["email"])
                    data = {
                        "title": subject,
                        "body": body,
                        "other_info": gettext("End"),
                    }
                    html = get_email_html(data)

                    send_email(subject=subject,
                               recipients=[user["email"]],
                               html_msg=html)
                    data = {
                        'msg':
                        gettext(
                            'Password reset successfully.Please return to login page to login'
                        ),
                        'msg_type':
                        's',
                        "http_status":
                        201
                    }
                    logout_user()
                else:
                    data = {
                        "msg_type": "w",
                        "msg": gettext("Reset password failed(unknown error)"),
                        "http_status": 400
                    }
                return data
    return data
예제 #11
0
def p_email_change(new_email_code, current_email_code, email, password):
    """
    用户邮箱修改
    :param code_group:(code_id, code)
    :param current_code_group:(current_email_code_id, current_email_code)
    :param email:
    :param password:
    :return:
    """
    # 验证当前邮箱收到的验证码,保证用户自己修改的
    if current_user.email:
        r = verify_code(current_email_code, current_user.email)
        if not r:
            oplog = {
                'op_type': 'set_email',
                'time': time.time(),
                'status': 'f',
                'info': 'Verification code mistake[currently bound]',
                'ip': request.remote_addr
            }

            insert_op_log(oplog)
            data = {
                "msg": gettext("Verification code error [currently bound]"),
                "msg_type": "w",
                "custom_status": 401
            }
            return data

    # 验证新邮箱收到的验证码,保证绑定的邮箱无误
    r = verify_code(new_email_code, email)
    if not r:
        oplog = {
            'op_type': 'set_email',
            'time': time.time(),
            'status': 'f',
            'info': 'Verification code mistake[ready to bind]',
            'ip': request.remote_addr
        }

        insert_op_log(oplog)
        data = {
            "msg": gettext("Verification code error [ready to bind]"),
            "msg_type": "w",
            "custom_status": 401
        }
        return data

    if current_user.verify_password(password) or not current_user.email:
        update_one_user(user_id=current_user.str_id,
                        updata={"$set": {
                            "email": email
                        }})
        oplog = {
            'op_type': 'set_email',
            'time': time.time(),
            'status': 's',
            'info': '',
            'ip': request.remote_addr
        }

        insert_op_log(oplog)
        data = {
            "msg": gettext("Email is changed"),
            "msg_type": "s",
            "custom_status": 201
        }
    else:
        oplog = {
            'op_type': 'set_email',
            'time': time.time(),
            'status': 'f',
            'info': 'Password mistake',
            'ip': request.remote_addr
        }
        insert_op_log(oplog)
        data = {
            'msg': gettext('Password mistake'),
            'msg_type': 'e',
            "custom_status": 401
        }

    return data
예제 #12
0
def content_inform():
    """
    内容举报
    :return:
    """

    ctype = request.argget.all('ctype')
    cid = request.argget.all('cid')
    category = request.argget.all('category')
    details = request.argget.all('details')

    s, r = arg_verify(reqargs=[("cid(content id)", cid)], required=True)
    if not s:
        return r

    s, r = arg_verify(reqargs=[("ctype(content type)", ctype)],
                      required=True, only=["user", "post", "comment", "media"])
    if not s:
        return r

    s, r = arg_verify(
        reqargs=[
            ("category", category)], only=[
            "ad", "junk_info", "plagiarize", "other"], required=True)
    if not s:
        return r

    if category == "other":
        s, r = arg_verify(
            reqargs=[
                (gettext("details"), details)], required=True)
        if not s:
            return r

    up_data = {
        "$inc": {"inform.{}.cnt".format(category): 1, "inform.total": 1},
        "$set": {"inform.update_time": time.time()}
    }

    if details:
        up_data["$addToSet"] = {"inform.{}.details".format(category): details}
    if ctype == "post":
        r = mdbs["web"].db.post.update_one({"_id": ObjectId(cid)}, up_data)
    elif ctype == "comment":
        r = mdbs["web"].db.comment.update_one({"_id": ObjectId(cid)}, up_data)

    elif ctype == "user":
        r = update_one_user(user_id=cid, updata=up_data)

    elif ctype == "media":
        r = mdbs["web"].db.media.update_one({"_id": ObjectId(cid)}, up_data)

    if r.modified_count:
        data = {
            'msg': gettext("Submitted successfully, thanks for your participation"),
            'msg_type': "s",
            "custom_status": 201}
    else:
        data = {'msg': gettext("Submit failed, please try again"),
                'msg_type': "w", "custom_status": 201}
    return data
예제 #13
0
def user_edit():
    """
    用户编辑
    :return:
    """
    tid = request.argget.all('id')
    role_id = request.argget.all('role_id')
    email = request.argget.all('email')
    password = request.argget.all('password')
    active = str_to_num(request.argget.all('active', 0))

    s, r = arg_verify(reqargs=[("id", tid), ("role_id", role_id)],
                      required=True)
    if not s:
        return r

    data = {
        'msg': gettext("Update success"),
        'msg_type': "s",
        "custom_status": 201
    }

    if not email:
        email = None
    update_data = {'role_id': role_id, 'active': active, "email": email}
    user = get_one_user(user_id=str(tid))
    if user:
        # 权限检查
        current_user_role = mdbs["user"].db.role.find_one(
            {"_id": ObjectId(current_user.role_id)})
        edit_user_role = mdbs["user"].db.role.find_one(
            {"_id": ObjectId(user["role_id"])})
        if edit_user_role \
                and get_num_digits(current_user_role["permissions"]) \
                <= get_num_digits(edit_user_role["permissions"]):
            # 没有权限修改
            data = {
                "msg_type": "w",
                "msg": gettext("No permission modification"),
                "custom_status": 401
            }
            return data

    if email:
        # 邮件注册
        # 邮箱格式验证
        s, r = email_format_ver(email)
        if not s:
            data = {'msg': r, 'msg_type': "e", "custom_status": 422}
            return data
        elif mdbs["user"].db.user.find_one({
                "email": email,
                "_id": {
                    "$ne": ObjectId(tid)
                }
        }):
            # 邮箱是否注册过
            data = {
                'msg':
                gettext(
                    "This email has been registered in the site oh, please login directly."
                ),
                'msg_type':
                "w",
                "custom_status":
                403
            }
            return data
    if password:
        # 密码格式验证
        s, r = password_format_ver(password)
        if not s:
            data = {'msg': r, 'msg_type': "e", "custom_status": 422}
            return data

    if password:
        password = generate_password_hash(password)
        update_data["password"] = password
    r = update_one_user(user_id=str(tid), updata={"$set": update_data})
    if not r.modified_count:
        data = {
            'msg': gettext("No changes"),
            'msg_type': "w",
            "custom_status": 201
        }
    return data
예제 #14
0
def avatar_upload():
    """
    头像上传
    :return:
    """
    result = None
    imgfile_base = request.argget.all("imgfile_base")
    max_size_mb = get_config("account", "USER_AVATAR_MAX_SIZE")
    max_size_b = max_size_mb * 1024 * 1024
    if imgfile_base:
        if len(imgfile_base) > max_size_b:
            data = {
                "msg":
                gettext("Upload avatar image can not exceed {}M".format(
                    max_size_mb)),
                "msg_type":
                "w",
                "http_status":
                413
            }
            return data
        else:
            result = fileup_base_64(uploaded_files=[imgfile_base],
                                    prefix="user_avatar/")
    else:
        file = request.files['upfile']
        if len(file.read()) > max_size_b:
            data = {
                "msg":
                gettext("Upload avatar image can not exceed {}M".format(
                    max_size_mb)),
                "msg_type":
                "w",
                "http_status":
                413
            }
            return data

        if file:
            tailoring = request.argget.all('tailoring')
            if tailoring:
                if not isinstance(tailoring, dict):
                    tailoring = json.loads(tailoring)
                for k in ["width", "height", "x", "y", "rotate"]:
                    tailoring.setdefault(k, 0)
            result = file_up(uploaded_files=[file],
                             prefix="user_avatar/",
                             tailoring=tailoring)

    data = {}
    if result:
        result = result[0]
        user = get_one_user(user_id=current_user.str_id)
        if user:
            if user['avatar_url'] and "key" in user['avatar_url'] \
                    and result["key"] != user['avatar_url']["key"]:
                # 当使用了不同的名字删除老的头像
                file_del(user['avatar_url'])

            update_data = {"avatar_url": result}
            r = update_one_user(user_id=current_user.str_id,
                                updata={"$set": update_data})
            if not r.matched_count:
                data = {
                    'msg': gettext("Save failed"),
                    'msg_type': "w",
                    "http_status": 400
                }
            else:
                if result["type"] == "local":
                    # 如果保存再本地的话, 保存为一定尺寸大小
                    path = "{}{}".format(APPS_PATH, get_file_url(result))
                    imgcp = ImageCompression(path, path)
                    ava_size = get_config("account", "USER_AVATAR_SIZE")
                    imgcp.custom_pixels(ava_size[0], ava_size[1])
                data = {
                    'msg': gettext("Save successfully"),
                    'msg_type': "s",
                    "http_status": 201
                }
    if not data:
        data = {
            'msg': gettext("Upload failed"),
            'msg_type': "w",
            "http_status": 400
        }

    # 清理user信息数据缓存
    delete_user_info_cache(user_id=current_user.str_id)
    return data
예제 #15
0
파일: sys_tool.py 프로젝트: dick7/osroom
def init_admin_user(mdbs):
    """
    初始化root用户角色, 管理员, 管理员基本资料
    :return:
    """
    from werkzeug.security import generate_password_hash
    from apps.modules.user.models.user import user_model
    from apps.modules.user.process.get_or_update_user import get_one_user_mfilter, update_one_user, insert_one_user

    print('\nInit root user')
    # 初始化角色
    root_per = SUPER_PER
    role_root = mdbs["user"].db.role.find_one({"permissions": root_per})
    if not role_root:
        print(" * Create root role...")
        r = mdbs["user"].db.role.insert_one(
            {
                "name": "Root",
                 "default": 0,
                 "permissions": root_per,
                 "instructions": 'Root'
            }
        )

        if r.inserted_id:
            print("Create root user role successfully")
        else:
            print("\033[31m[Error] Failed to create superuser role\033[0m")
            sys.exit(-1)

        root_id = r.inserted_id
    else:
        root_id = role_root['_id']

    root_user = mdbs["user"].dbs["user"].find_one(
        {"role_id": str(root_id)},
        {
            "username": 1,
            "email": 1
        }
    )

    if root_user:
        ch = input("\033[33m\n Root user already exists, need to update its password?[Y/n]\033[0m")
        if ch != "Y":
            print("End")
            sys.exit()
        is_continue = False
        while not is_continue:
            password = getpass("Input password(Password at least 8 characters):")
            if len(password) < 8:
                print("\033[33m[Warning]: {}The password is at least 8 characters\033[0m\n")
            else:
                break
        password_hash = generate_password_hash(password)
        update_one_user(
            user_id=str(root_user["_id"]),
            updata={
                "$set": {
                    "password": password_hash
                }
            })
        username = root_user["username"]
        email = root_user["email"]

    else:
        is_continue = False
        username = "******"
        email = input("Input email:")
        while not is_continue:
            password = getpass("Input password(Password at least 8 characters):")
            if len(password) < 8:
                print("\033[33m[Warning]: {}The password is at least 8 characters\033[0m\n")
            else:
                break
        try:
            mdbs["user"].db.create_collection("role")
            print(' * Created role collection')
        except BaseException:
            pass
        try:
            mdbs["user"].db.create_collection("user")
            print(' * Created user collection')
        except BaseException:
            pass

        password_hash = generate_password_hash(password)
        user = get_one_user_mfilter(email=email, op="or")
        if user:
            update_one_user(user_id=str(user["_id"]),
                            updata={
                                "$set": {
                                    "password": password_hash,
                                    "role_id": str(root_id)
                            }
            })
            username = user["username"]
            print("\033[33m\n * This user already exists, updated password and role.\033[0m")
        else:
            print(' * Create root user...')
            user = user_model(
                username=username,
                email=email,
                password=password,
                custom_domain=-1,
                role_id=str(root_id),
                active=True)
            r = insert_one_user(updata=user)
            if r.inserted_id:
                print(" * Create a root user successfully")
            else:
                print("\033[31m * [Error] Failed to create a root user\033[0m")
                sys.exit(-1)

    # To create the average user role
    average_user = mdbs["user"].db.role.find_one({"permissions": 1})
    if not average_user:
        print(" * Create the average user role...")
        r = mdbs["user"].db.role.insert_one({
            "name": "User",
            "default": 1,
            "permissions": 1,
            "instructions": 'The average user',
        })
        if r.inserted_id:
            print(" * Create a generic role successfully")
        else:
            print(" * Failed to create a generic role")

    role = mdbs["user"].db.role.find_one({"_id": root_id})
    hidden_password = "******".format(password[0:2], password[6:])
    print('\nThe basic information is as follows')
    print('Username: {}\nEmail: {}\nUser role: {}\nPassword: \033[33m{}\033[0m'.format(
        username, email, role["name"], hidden_password))
    print('End')
    sys.exit()