确定当前用户是否属于管理员组(Windows/Python)

发布于 2024-08-25 17:41:44 字数 85 浏览 6 评论 0原文

我希望只有当前用户是管理员时才能访问应用程序中的某些功能。

如何在 Windows 上使用 Python 确定当前用户是否属于本地管理员组?

I want certain functions in my application to only be accessible if the current user is an administrator.

How can I determine if the current user is in the local Administrators group using Python on Windows?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戏舞 2024-09-01 17:41:44

您可以尝试这个

import ctypes
print ctypes.windll.shell32.IsUserAnAdmin()

You could try this:

import ctypes
print ctypes.windll.shell32.IsUserAnAdmin()
弃爱 2024-09-01 17:41:44
import win32net


def if_user_in_group(group, member):
    members = win32net.NetLocalGroupGetMembers(None, group, 1)
    return member.lower() in list(map(lambda d: d['name'].lower(), members[0]))  


# Function usage
print(if_user_in_group('SOME_GROUP', 'SOME_USER'))

当然,在您的情况下,“SOME_GROUP”将是“管理员”

import win32net


def if_user_in_group(group, member):
    members = win32net.NetLocalGroupGetMembers(None, group, 1)
    return member.lower() in list(map(lambda d: d['name'].lower(), members[0]))  


# Function usage
print(if_user_in_group('SOME_GROUP', 'SOME_USER'))

Of course in your case 'SOME_GROUP' will be 'administrators'

人海汹涌 2024-09-01 17:41:44

我想对 Vlad Bezden 给予一些信任,因为如果没有他使用 win32net 模块,这里的答案将是不存在。

如果您确实想知道用户是否有能力过去的 UAC 充当管理员,您可以执行以下操作。如果需要,它还列出当前用户所在的组。
它将适用于大多数(所有?)语言设置
本地组只需以“Admin”开头,通常这样做...
(有人知道某些设置是否会有所不同吗?)

要使用此代码片段,您需要有已安装 pywin32 模块,
如果您还没有,您可以从 PyPI 获取它: pip install pywin32

重要须知:

对于某些用户/编码人员来说,函数 os.getlogin() 仅自 Windows 操作系统上的 python3.1 起可用...
python3.1 文档

win32net 参考

from time import sleep
import os
import win32net

if 'logonserver' in os.environ:
    server = os.environ['logonserver'][2:]
else:
    server = None

def if_user_is_admin(Server):
    groups = win32net.NetUserGetLocalGroups(Server, os.getlogin())
    isadmin = False
    for group in groups:
        if group.lower().startswith('admin'):
            isadmin = True
    return isadmin, groups


# Function usage
is_admin, groups = if_user_is_admin(server)

# Result handeling
if is_admin == True:
    print('You are a admin user!')
else:
    print('You are not an admin user.')
print('You are in the following groups:')
for group in groups:
    print(group)

sleep(10)

# (C) 2018 DelphiGeekGuy@Stack Overflow
# Don't hesitate to credit the author if you plan to use this snippet for production.

哦,还有哪里 from time import sleepsleep(10):

插入自己的导入/代码...

I'd like to give some credit to Vlad Bezden, becuase without his use of the win32net module this answer here would not exist.

If you really want to know if the user has the ability to act as an admin past UAC you can do the following. It also lists the groups the current user is in, if needed.
It will work on most (all?) language set-ups.
The local group just hast to start with "Admin", which it usually does...
(Does anyone know if some set-ups will be different?)

To use this code snippet you'll need to have pywin32 module installed,
if you don't have it yet you can get it from PyPI: pip install pywin32

IMPORTANT TO KNOW:

It may be important to some users / coders that the function os.getlogin() is only available since python3.1 on Windows operating systems...
python3.1 Documentation

win32net Reference

from time import sleep
import os
import win32net

if 'logonserver' in os.environ:
    server = os.environ['logonserver'][2:]
else:
    server = None

def if_user_is_admin(Server):
    groups = win32net.NetUserGetLocalGroups(Server, os.getlogin())
    isadmin = False
    for group in groups:
        if group.lower().startswith('admin'):
            isadmin = True
    return isadmin, groups


# Function usage
is_admin, groups = if_user_is_admin(server)

# Result handeling
if is_admin == True:
    print('You are a admin user!')
else:
    print('You are not an admin user.')
print('You are in the following groups:')
for group in groups:
    print(group)

sleep(10)

# (C) 2018 DelphiGeekGuy@Stack Overflow
# Don't hesitate to credit the author if you plan to use this snippet for production.

Oh and WHERE from time import sleep and sleep(10):

INSERT own imports/code...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文