Ejemplo n.º 1
0
def user_change_password(request):
    """
    修改用户密码
    :param request:
    :return:
    """
    try:
        request_data = common.print_header_data(request)
        user_data = lc.trans_user_change_password(request_data)
        print user_data
        username = user_data['username']
        user = auth.authenticate(**{
            'username': username,
            'password': user_data['old_password']
        })
        if user is not None:
            user.set_password(user_data['new_password'])
            user.save()
            User.objects.filter(username=username).update(
                last_update_time=du.get_current_time())
            common.generate_system_log(request_data, u' 用户操作', u'修改密码操作',
                                       username + u'修改密码成功')
            return common.ui_message_response(200, '修改密码成功', "success",
                                              status.HTTP_200_OK)
        else:
            common.generate_system_log(request_data, u' 用户操作', u'修改密码操作',
                                       username + u'修改密码失败')
            return common.ui_message_response(400, '原密码输入错误', '原密码输入错误')
    except Exception:
        traceback.print_exc()
        return common.ui_message_response(
            500, '服务器内部错误', '服务器内部错误', status.HTTP_500_INTERNAL_SERVER_ERROR)
Ejemplo n.º 2
0
def reset_user_password(request):
    """
    重置用户密码
    :param request:
    :return:
    """
    try:
        request_data = common.print_header_data(request)
        user_data = lc.trans_user_change_password(request_data)
        user = User.objects.get(id=user_data['id'])
        username = user.username
        if user is not None:
            user.set_password(str(su.get_md5('123456')).upper())
            user.save()
            User.objects.filter(username=username).update(
                last_update_time=du.get_current_time())
            common.generate_system_log(request_data, u' 用户操作', u'重置密码操作',
                                       username + u'重置密码成功')
            return common.ui_message_response(200, '修改密码成功', "success",
                                              status.HTTP_200_OK)
        else:
            common.generate_system_log(request_data, u' 用户操作', u'重置密码操作',
                                       username + u'重置密码失败')
            return common.ui_message_response(400, '用户不存在', '用户不存在')
    except Exception:
        traceback.print_exc()
        return common.ui_message_response(
            500, '服务器内部错误', '服务器内部错误', status.HTTP_500_INTERNAL_SERVER_ERROR)
Ejemplo n.º 3
0
def user_login(request):
    """
    登录校验
    :param request:
    :return:
    """
    try:
        result = {}
        user_data = {}
        role_result = {}
        request_data = common.print_header_data(request)
        username = request_data.get('username')
        # user_data['username'] = '******'
        # username = '******'
        user_data['username'] = username
        password = request_data.get('password')
        # user_data['password'] = str(su.get_md5('123456')).upper()
        # password = str(su.get_md5('123456')).upper()
        user_data['password'] = password
        if User.objects.filter(username=username):
            user = auth.authenticate(**user_data)
            if user:
                # django.contrib.auth.login(request, user)
                result['username'] = user_data['username']
                user_query = User.objects.filter(username=result['username'])
                user_query.update(last_login=du.get_current_time())
                group_query = user_query[0].groups.all()
                for group in group_query:
                    permission_id_list = []
                    permission_query = group.permissions.all()
                    for permission in permission_query:
                        permission_id_list.append(permission.id)
                    role_result[group.name] = permission_id_list

                result['role'] = role_result
                common.generate_system_log(request_data, u'用户操作', u'登录操作',
                                           username + u'登录成功')
                result['flag'] = "success"
                print "login result:", result
                return common.ui_message_response(200, '登录成功', result,
                                                  status.HTTP_200_OK)
            else:
                common.generate_system_log(request_data, u'用户操作', u'登录操作',
                                           username + u'密码错误')
                return common.ui_message_response(400, '密码错误', '密码错误')
        else:
            common.generate_system_log(request_data, u'用户操作', u'登录操作',
                                       u'用户名不存在')
            return common.ui_message_response(400, '用户名不存在', '用户名不存在')
    except Exception:
        traceback.print_exc()
        common.generate_system_log(request_data, u'用户操作', u'登录操作', u'登录模块出现异常')
        return common.ui_message_response(
            500, '服务器内部错误', '服务器内部错误', status.HTTP_500_INTERNAL_SERVER_ERROR)
Ejemplo n.º 4
0
def user_logout(request):
    """
    用户注销
    :param request:
    :return:
    """
    # logout(request)
    try:
        request_data = common.print_header_data(request)  # 获取请求数据

        # 获取请求参数
        login_id = request_data.get('username')
        if login_id is not None:
            common.generate_system_log(request_data, u'用户操作', u'注销操作',
                                       login_id + u'注销成功')
            return common.ui_message_response(200, '注销成功', '注销成功',
                                              status.HTTP_200_OK)
        else:
            common.generate_system_log(request_data, u'用户操作', u'注销操作',
                                       u'用户名未知')
            return common.ui_message_response(400, '用户名参数为空', "用户名参数为空")

    except Exception:
        common.generate_system_log(request_data, u'用户操作', u'注销操作', u'注销模块出现异常')
        traceback.print_exc()
        return common.ui_message_response(
            500, '服务器内部错误', '服务器内部错误', status.HTTP_500_INTERNAL_SERVER_ERROR)
Ejemplo n.º 5
0
def user_registration(request):
    """
    注册用户信息,创建用户名和密码,auth模块不存储用户密码明文而是存储一个Hash值, 比如迭代使用Md5算法.
    :param request:
    :return:
    """
    try:
        request_data = common.print_header_data(request)
        user_data = lc.trans_user_registration_info(request_data)
        if 'password' not in user_data:
            user_data['password'] = str(su.get_md5('123456')).upper()
        user_name = user_data['username']
        if 'role_id' in user_data:
            role_id = user_data['role_id']
            del user_data['role_id']
        else:
            role_id = 3  # 默认角色
        user_data['is_active'] = '1'
        user_data['is_staff'] = '1'
        print "user_data:", user_data
        if user_data:
            query_data = User.objects.filter(username=user_name)
            if query_data.exists():
                common.generate_system_log(request_data, u'用户操作', u'注册操作',
                                           user_name + u'注册失败,用户名被占用')
                return common.ui_message_response(400, '用户名已经被占用', "用户名已经被占用")
            else:
                user = User.objects.create_user(**user_data)
                user.save()
                if role_id:
                    group_select = Group.objects.filter(id=role_id)
                    for group in group_select:
                        user.groups.add(group)
                common.generate_system_log(request_data, u'用户操作', u'注册操作',
                                           user_name + u'注册成功')
                return common.ui_message_response(200, '用户注册成功', "success",
                                                  status.HTTP_200_OK)
    except Exception:
        traceback.print_exc()
        common.generate_system_log(request_data, u' 用户操作', u'注册操作',
                                   user_name + u'注册失败,注册出现错误')
        return common.ui_message_response(
            500, '服务器内部错误', '服务器内部错误', status.HTTP_500_INTERNAL_SERVER_ERROR)