简体   繁体   English

确定当前用户是否在Administrators组中(Windows / Python)

[英]Determine if current user is in Administrators group (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? 如何在Windows上使用Python确定当前用户是否在本地Administrators组中?

You could try this : 你可以试试这个

import ctypes
print ctypes.windll.shell32.IsUserAnAdmin()
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' 当然在你的情况下'SOME_GROUP'将是'管理员'

I'd like to give some credit to Vlad Bezden , becuase without his use of the win32net module this answer here would not exist. 我想给Vlad Bezden一些信任,因为没有使用win32net模块,这里的答案就不存在了。

If you really want to know if the user has the ability to act as an admin past UAC you can do the following. 如果您真的想知道用户是否能够在UAC之前充当管理员,您可以执行以下操作。 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... 本地组只需要从“Admin”开始,它通常会......
(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 要使用此代码片段,您需要安装pywin32模块,如果您还没有,可以从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 某些用户/编码人员可能很重要的是, os.getlogin()函数仅在Windows操作系统上的python3.1之后才可用... python3.1文档

win32net Reference 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.

Oh and WHERE from time import sleep and sleep(10) : 哦, from time import sleep哪里from time import sleepsleep(10)

INSERT own imports/code... INSERT自己的导入/代码......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM