#NEWLINE# def _win_user_token_is_admin( user_token ) : #NEWLINE# #INDENT# "\n    Using the win32 api, determine if the user with token 'user_token' has\n    administrator rights.\n\n    See MSDN entry here:\n        http://msdn.microsoft.com/en-us/library/aa376389(VS.85).aspx\n    " #NEWLINE# class SID_IDENTIFIER_AUTHORITY ( ctypes . Structure ) : #NEWLINE# #INDENT# _fields_ = [ ( 'byte0' , ctypes . c_byte ) , ( 'byte1' , ctypes . c_byte ) , ( 'byte2' , ctypes . c_byte ) , ( 'byte3' , ctypes . c_byte ) , ( 'byte4' , ctypes . c_byte ) , ( 'byte5' , ctypes . c_byte ) ] #NEWLINE# #UNINDENT# nt_authority = SID_IDENTIFIER_AUTHORITY ( ) #NEWLINE# nt_authority . byte5 = 5 #NEWLINE# SECURITY_BUILTIN_DOMAIN_RID = 32 #NEWLINE# DOMAIN_ALIAS_RID_ADMINS = 544 #NEWLINE# administrators_group = ctypes . c_void_p ( ) #NEWLINE# if ( ctypes . windll . advapi32 . AllocateAndInitializeSid ( ctypes . byref ( nt_authority ) , 2 , SECURITY_BUILTIN_DOMAIN_RID , DOMAIN_ALIAS_RID_ADMINS , 0 , 0 , 0 , 0 , 0 , 0 , ctypes . byref ( administrators_group ) ) == 0 ) : #NEWLINE# #INDENT# raise Exception ( 'AllocateAndInitializeSid failed' ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# is_admin = ctypes . wintypes . BOOL ( ) #NEWLINE# if ( ctypes . windll . advapi32 . CheckTokenMembership ( user_token , administrators_group , ctypes . byref ( is_admin ) ) == 0 ) : #NEWLINE# #INDENT# raise Exception ( 'CheckTokenMembership failed' ) #NEWLINE# #UNINDENT# return ( is_admin . value != 0 ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# ctypes . windll . advapi32 . FreeSid ( administrators_group )