home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / ScreenResolution / policykit.py next >
Encoding:
Python Source  |  2009-04-06  |  3.1 KB  |  96 lines

  1. # -*- coding: utf-8 -*-
  2. # Copyright (C) 2008 Fluendo Embedded S.L. (www.fluendo.com)
  3. #
  4. # This program is free software; you can redistribute it and/or modify it under
  5. # the terms of the GNU General Public License as published by the Free Software
  6. # Foundation; either version 2 of the License, or (at your option) any later
  7. # version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but WITHOUT
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  12. # details.
  13. #
  14. # You should have received a copy of the GNU General Public License along with
  15. # this program; if not, write to the Free Software Foundation, Inc.,
  16. # 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  17. #
  18. # Taken from:
  19. # https://code.fluendo.com/remotecontrol/trac/browser/trunk/
  20. #         gnome_lirc_properties/policykit.py?rev=197
  21. #
  22. # modified by Harald Hoyer <harald@redhat.com>
  23. '''
  24. Helper class to do authentication via PolicyKit
  25. '''
  26.  
  27. import dbus
  28. import os
  29. import logging
  30.  
  31. class PolicyKitAuthentication(object):
  32.     '''
  33.     Helper class to do authentication via PolicyKit
  34.     '''
  35.     def __init__(self):
  36.         super(PolicyKitAuthentication, self).__init__()
  37.         self.__pk = None
  38.         self.__pa = None
  39.  
  40.     def is_authorized(self, action_id, pid=None):
  41.         '''
  42.         Ask PolicyKit whether we are already authorized.
  43.         '''
  44.  
  45.         # Check whether the process is authorized:
  46.         if pid == None:
  47.             pid = os.getpid()
  48.             
  49.         pid = dbus.UInt32(pid)
  50.         authorized = self.policy_kit.IsProcessAuthorized(action_id, pid, False)
  51.         logging.debug('%s: authorized=%r', action_id, authorized)
  52.  
  53.         return ('yes' == authorized)
  54.  
  55.     def obtain_authorization(self, action_id, widget=None):
  56.         '''
  57.         Try to obtain authoriztation for the specified action.
  58.         '''
  59.         if self.is_authorized(action_id):
  60.             return True
  61.  
  62.         xid = (widget and widget.get_toplevel().window.xid or 0)
  63.         xid, pid = dbus.UInt32(xid), dbus.UInt32(os.getpid())
  64.  
  65.         granted = self.auth_agent.ObtainAuthorization(action_id, xid, pid)
  66.         logging.debug('%s: granted=%r', action_id, granted)
  67.  
  68.         return bool(granted)
  69.  
  70.     def __get_policy_kit(self):
  71.         '''Retreive the D-Bus interface of PolicyKit.'''
  72.  
  73.         if self.__pk:
  74.             return self.__pk
  75.  
  76.         # retreiving the interface raises DBusException on error:
  77.         service = dbus.SystemBus().get_object('org.freedesktop.PolicyKit', '/')
  78.         self.__pk = dbus.Interface(service, 'org.freedesktop.PolicyKit')
  79.         return self.__pk        
  80.  
  81.     def __get_auth_agent(self):
  82.         '''
  83.         Retreive the D-Bus interface of the PolicyKit 
  84.         authentication agent.
  85.         '''
  86.  
  87.         if self.__pa:
  88.             return self.__pa
  89.         # retreiving the interface raises DBusException on error:
  90.         self.__pa = dbus.SessionBus().get_object(
  91.             'org.freedesktop.PolicyKit.AuthenticationAgent', '/')
  92.         return self.__pa
  93.  
  94.     auth_agent = property(__get_auth_agent)
  95.     policy_kit = property(__get_policy_kit)
  96.