Package Products :: Package ZenModel :: Module ZentinelPortal |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenModel.ZentinelPortal

      1  ##############################################################################
      2  #
      3  # Copyright (C) Zenoss, Inc. 2007, all rights reserved.
      4  #
      5  # This content is made available according to terms specified in
      6  # License.zenoss under the directory where your Zenoss product is installed.
      7  #
      8  ##############################################################################
      9
     10
     11  """ Portal class
     12
     13  $Id: ZentinelPortal.py,v 1.17 2004/04/08 15:35:25 edahl Exp $
     14  """
     15
     16  import urllib, urlparse
     17  import re
     18
     19  import Globals
     20
     21  from zExceptions import Redirect
     22  from AccessControl import getSecurityManager, ClassSecurityInfo
     23
     24  from Products.Sessions.BrowserIdManager import constructBrowserIdManager
     25  from Products.Sessions.SessionDataManager import constructSessionDataManager
     26
     27  from Products.CMFCore.PortalObject import PortalObjectBase
     28  from Products.CMFCore.utils import getToolByName
     29
     30  from Products.Zuul.interfaces import ICatalogTool
     31  from Products.ZenUtils import Security, Time
     32  from Products.ZenUtils.Utils import prepId
     33  from Products.ZenUtils.deprecated import deprecated
     34  from Products.ZenUI3.browser.interfaces import IErrorMessage
     35
     36  from ZenossSecurity import *
     37
     38  from Products.AdvancedQuery import MatchGlob, Eq, Or
     39
     40 -class ZentinelPortal ( PortalObjectBase ):
     41      """
     42      The *only* function this class should have is to help in the setup
     43      of a new ZentinelPortal. It should not assist in the functionality at all.
     44      """
     45      meta_type = 'ZentinelPortal'
     46
     47      _properties = (
     48          {'id':'title', 'type':'string'},
     49          {'id':'description', 'type':'text'},
     50          )
     51      title = ''
     52      description = ''
     53
     54      security = ClassSecurityInfo()
     55
     56 -    def __init__( self, id, title='' ):
     57          PortalObjectBase.__init__( self, id, title )
     58
     59 -    def server_time(self):
     60          return Time.isoDateTime()
     61
     62 -    def _additionalQuery(self):
     63          return None
     64
     65      security.declareProtected(ZEN_COMMON, 'searchDevices')
     66      @deprecated
     67 -    def searchDevices(self, queryString='', REQUEST=None):
     68          """Returns the concatenation of a device name, ip and mac
     69          search on the list of devices.
     70          """
     71          # TODO: Remove. Not used anymore in Zenoss code --Ian
     72          zcatalog = self.dmd.Devices.deviceSearch
     73          glob = queryString.rstrip('*') + '*'
     74          idGlob = MatchGlob('id', glob)
     75          titleGlob = MatchGlob('titleOrId', glob)
     76          idOrTitleQuery = Or(idGlob,titleGlob)
     77          query = Or(idOrTitleQuery, Eq('getDeviceIp', queryString))
     78          additionalQuery = self._additionalQuery()
     79          if additionalQuery:
     80              query = And( query, additionalQuery )
     81          brains = zcatalog.evalAdvancedQuery(query)
     82          if REQUEST and len(brains) == 1:
     83              raise Redirect(urllib.quote(brains[0].getPrimaryId))
     84          if additionalQuery:
     85              idGlob = And( idGlob, additionalQuery )
     86          brains += self.dmd.Networks.ipSearch.evalAdvancedQuery(idGlob)
     87          return [ b.getObject() for b in brains ]
     88
     89      security.declareProtected(ZEN_COMMON, 'searchComponents')
     90      @deprecated
     91 -    def searchComponents(self, device='', component='', REQUEST=None):
     92          """
     93          Redirect to the component of a device. Hopefully.
     94          """
     95          # TODO: Remove. Not used anymore in Zenoss code --Ian
     96          catalog = self.dmd.Devices.componentSearch
     97          brains = []
     98          if device and component:
     99              brains = catalog(getParentDeviceName=device)
    100          matchingBrains = []
    101          if brains:
    102              component = prepId(component)
    103              for brain in brains:
    104                  if brain.getPath().split('/')[-1]==component:
    105                      if REQUEST:
    106                          raise Redirect(urllib.quote(
    107                              brain.getPath()+'/viewEvents'))
    108                      else:
    109                          matchingBrains.append(brain)
    110              if REQUEST and len(matchingBrains) == 0:
    111                  return self.searchDevices(device, REQUEST)
    112          return [b.getObject() for b in matchingBrains]
    113
    114      security.declareProtected(ZEN_COMMON, 'dotNetProxy')
    115 -    def dotNetProxy(self, path='', params={}, REQUEST=None):
    116          """
    117          Logs in to Zenoss.net using the user's credentials and retrieves data,
    118          thereby putting it in the current domain
    119          """
    120          session = self.dmd.ZenUsers.getUserSettings().getDotNetSession()
    121          response = session.open(path.lstrip('/'))
    122          if response:
    123              data = response.read()
    124              headers = response.headers.dict
    125              url = response.geturl()
    126              response.close()
    127          else:
    128              return response
    129          localbase = 'http://localhost:8080/zport/dotNetProxy?path='
    130          allrefs = re.compile(r"""(href *= *["']|src *= *["'])(.*?)(["'])""")
    131          proxyrefs = re.compile(
    132              r"""((<a[^<>]*?|location\.)href *= *["'])(.*?)(['"])""")
    133
    134          def mod_rewrite(matchobj):
    135              start, path, end = matchobj.groups()
    136              if not path.startswith('javascript'):
    137                  path = urlparse.urljoin(url, path)
    138              return start + path + end
    139
    140          def make_proxied(matchobj):
    141              start, trash, path, end = matchobj.groups()
    142              path = path.replace(session.base_url, localbase)
    143              return start + path + end
    144
    145          data = re.sub(allrefs, mod_rewrite, data)
    146          data = re.sub(proxyrefs, make_proxied, data)
    147          for header in headers:
    148              REQUEST.RESPONSE.setHeader(header, headers[header])
    149          return data
    150
    151 -    def isManager(self, obj=None):
    152          """
    153          Return true if user is authenticated and has Manager role.
    154          """
    155          user = self.dmd.ZenUsers.getUser()
    156          if user:
    157              return user.has_role((MANAGER_ROLE, ZEN_MANAGER_ROLE), obj)
    158
    159
    160 -    def has_role(self, role, obj=None):
    161          """Check to see of a user has a role.
    162          """
    163          if obj is None: obj = self
    164          user = getSecurityManager().getUser()
    165          if user: return user.has_role(role, obj)
    166
    167
    168 -    def has_permission(self, perm, obj=None):
    169          """Check to see of a user has a permission.
    170          """
    171          if obj is None: obj = self
    172          user = getSecurityManager().getUser()
    173          if user: return user.has_permission(perm, obj)
    174
    175 -    def getZenossVersionShort(self):
    176          return self.About.getZenossVersionShort()
    177
    178 -    def getVersionedResourcePath(self, path):
    179          from Products.ZenUI3.browser.javascript import getVersionedPath
    180          return getVersionedPath(path)
    181
    182 -    def getLoginButton(self):
    183          return """<input id="loginButton" type="submit" name="submitbutton"
    184                  class="submitbutton" value=""/>"""
    185
    186 -    def getExtraLoginFormContents(self):
    187          """
    188          On first run, log us in as admin automatically.
    189
    190          This is done via a proxy form with hidden fields, so that the browser
    191          doesn't ask to save the password (which will be changed on the next
    192          screen).
    193          """
    194          if not self.dmd._rq:
    195              return """
    196              <form id="_proxy_form">
    197              <input type="hidden" name="__ac_name"/>
    198              <input type="hidden" name="__ac_password"/>
    199              <input type="hidden" name="came_from" value="/zport/dmd/quickstart"/>
    200              </form>
    201              <script>
    202              var origform=document.forms[0];
    203              var newform = document.getElementById('_proxy_form');
    204              newform.__ac_name.value = 'admin';
    205              newform.__ac_password.value = 'zenoss';
    206              newform.action = origform.action;
    207              newform.method = origform.method;
    208              newform.submit()
    209              </script>
    210              """
    211
    212
    213  Globals.InitializeClass(ZentinelPortal)
    214
    215
    216 -class PortalGenerator:
    217
    218      klass = ZentinelPortal
    219
    220 -    def setupTools(self, p):
    221          """Set up initial tools"""
    222          addCMFCoreTool = p.manage_addProduct['CMFCore'].manage_addTool
    223          addCMFCoreTool('CMF Skins Tool', None)
    224
    225
    226 -    def setupMailHost(self, p):
    227          p.manage_addProduct['MailHost'].manage_addMailHost(
    228              'MailHost', smtp_host='localhost')
    229
    230
    231 -    def setupUserFolder(self, p):
    232          #p.manage_addProduct['OFSP'].manage_addUserFolder()
    233          Security.createPASFolder(p)
    234          Security.setupPASFolder(p)
    235
    236
    237 -    def setupCookieAuth(self, p):
    238          # XXX PAS is handling this now, right?
    239          #p.manage_addProduct['CMFCore'].manage_addCC(
    240          #    id='cookie_authentication')
    241          pass
    242
    243
    244 -    def setupRoles(self, p):
    245          # Set up the suggested roles.
    246          p.__ac_roles__ += (ZEN_USER_ROLE, ZEN_MANAGER_ROLE,)
    247
    248
    249 -    def setupPermissions(self, p):
    250          # Set up some suggested role to permission mappings.
    251          mp = p.manage_permission
    252          mp(ZEN_CHANGE_SETTINGS,[ZEN_MANAGER_ROLE, OWNER_ROLE, MANAGER_ROLE,], 1)
    253          mp(ZEN_CHANGE_DEVICE, [ZEN_MANAGER_ROLE, OWNER_ROLE, MANAGER_ROLE,], 1)
    254          mp(ZEN_CHANGE_DEVICE_PRODSTATE,
    255              [ZEN_MANAGER_ROLE, OWNER_ROLE, MANAGER_ROLE,], 1)
    256          mp(ZEN_MANAGE_DMD, [ZEN_MANAGER_ROLE, OWNER_ROLE, MANAGER_ROLE,], 1)
    257          mp(ZEN_DELETE, [ZEN_MANAGER_ROLE, OWNER_ROLE, MANAGER_ROLE,], 1)
    258          mp(ZEN_DELETE_DEVICE, [ZEN_MANAGER_ROLE, OWNER_ROLE, MANAGER_ROLE,], 1)
    259          mp(ZEN_ADD, [ZEN_MANAGER_ROLE, OWNER_ROLE, MANAGER_ROLE,], 1)
    260          mp(ZEN_VIEW, [ZEN_USER_ROLE, ZEN_MANAGER_ROLE,
    261                          MANAGER_ROLE, OWNER_ROLE])
    262          mp(ZEN_COMMON, ["Authenticated", ZEN_USER_ROLE, ZEN_MANAGER_ROLE,
    263                          MANAGER_ROLE, OWNER_ROLE], 1)
    264
    265          # Events
    266          mp(ZEN_MANAGE_EVENTMANAGER,
    267              [ZEN_MANAGER_ROLE, OWNER_ROLE, MANAGER_ROLE,], 1)
    268          mp(ZEN_MANAGE_EVENTS,
    269              [ZEN_MANAGER_ROLE, OWNER_ROLE, MANAGER_ROLE,], 1)
    270          mp(ZEN_SEND_EVENTS,
    271              [ZEN_MANAGER_ROLE, OWNER_ROLE, MANAGER_ROLE,], 1)
    272
    273          mp(ZEN_CHANGE_ALERTING_RULES,
    274              [ZEN_MANAGER_ROLE, MANAGER_ROLE, OWNER_ROLE], 1)
    275          mp(ZEN_CHANGE_ADMIN_OBJECTS, [ZEN_MANAGER_ROLE, MANAGER_ROLE], 1)
    276          mp(ZEN_CHANGE_EVENT_VIEWS, [ZEN_MANAGER_ROLE, MANAGER_ROLE], 1)
    277          mp(ZEN_ADMIN_DEVICE, [ZEN_MANAGER_ROLE, MANAGER_ROLE], 1)
    278          mp(ZEN_MANAGE_DEVICE, [ZEN_MANAGER_ROLE, MANAGER_ROLE], 1)
    279          mp(ZEN_ZPROPERTIES_EDIT, [ZEN_MANAGER_ROLE, MANAGER_ROLE], 1)
    280          mp(ZEN_ZPROPERTIES_VIEW,
    281              [ZEN_MANAGER_ROLE, MANAGER_ROLE, ZEN_USER_ROLE], 1)
    282          mp(ZEN_EDIT_LOCAL_TEMPLATES, [ZEN_MANAGER_ROLE, MANAGER_ROLE], 1)
    283          mp(ZEN_RUN_COMMANDS, [ZEN_USER_ROLE, ZEN_MANAGER_ROLE, MANAGER_ROLE], 1)
    284          mp(ZEN_DEFINE_COMMANDS_EDIT, [MANAGER_ROLE], 1)
    285          mp(ZEN_DEFINE_COMMANDS_VIEW,
    286              [ZEN_MANAGER_ROLE, MANAGER_ROLE, ZEN_USER_ROLE], 1)
    287          mp(ZEN_MAINTENANCE_WINDOW_EDIT, [ZEN_MANAGER_ROLE, MANAGER_ROLE], 1)
    288          mp(ZEN_MAINTENANCE_WINDOW_VIEW,
    289              [ZEN_MANAGER_ROLE, MANAGER_ROLE, ZEN_USER_ROLE], 1)
    290          mp(ZEN_ADMINISTRATORS_EDIT, [ZEN_MANAGER_ROLE, MANAGER_ROLE], 1)
    291          mp(ZEN_ADMINISTRATORS_VIEW,
    292              [ZEN_MANAGER_ROLE, MANAGER_ROLE, ZEN_USER_ROLE], 1)
    293          #mp(ZEN_EDIT_USER, [ZEN_MANAGER_ROLE, MANAGER_ROLE], 1)
    294          #mp(ZEN_EDIT_USERGROUP, [ZEN_MANAGER_ROLE, MANAGER_ROLE], 1)
    295
    296 -    def setupDefaultSkins(self, p):
    297          from Products.CMFCore.DirectoryView import addDirectoryViews
    298          ps = getToolByName(p, 'portal_skins')
    299          addDirectoryViews(ps, 'skins', globals())
    300          ps.manage_addProduct['OFSP'].manage_addFolder(id='custom')
    301          ps.addSkinSelection('Basic', "custom, zenmodel", make_default=1)
    302          p.setupCurrentSkin()
    303
    304
    305 -    def setupSessionManager(self, p):
    306          """build a session manager and brower id manager for zport"""
    307          constructBrowserIdManager(p, cookiepath="/zport")
    308          constructSessionDataManager(p, "session_data_manager",
    309                      title="Session Data Manager",
    310                      path='/temp_folder/session_data')
    311
    312
    313 -    def setup(self, p, create_userfolder):
    314          if create_userfolder:
    315              self.setupUserFolder(p)
    316          #self.setupCookieAuth(p)
    317          self.setupTools(p)
    318          self.setupMailHost(p)
    319          self.setupRoles(p)
    320          self.setupPermissions(p)
    321          self.setupDefaultSkins(p)
    322          self.setupSessionManager(p)
    323
    324
    325 -    def create(self, parent, id, create_userfolder):
    326          id = str(id)
    327          portal = self.klass(id=id)
    328          parent._setObject(id, portal)
    329          # Return the fully wrapped object.
    330          p = parent.this()._getOb(id)
    331          self.setup(p, create_userfolder)
    332          return p
    333
    334
    335 -    def setupDefaultProperties(self, p, title, description,
    336                                 email_from_address, email_from_name,
    337                                 validate_email,
    338                                 ):
    339          p._setProperty('email_from_address', email_from_address, 'string')
    340          p._setProperty('email_from_name', email_from_name, 'string')
    341          p._setProperty('validate_email', validate_email and 1 or 0, 'boolean')
    342          p.title = title
    343          p.description = description
    344
    345
    346  manage_addZentinelPortal = Globals.HTMLFile('dtml/addPortal', globals())
    347  manage_addZentinelPortal.__name__ = 'addPortal'
    348
    349 -def manage_addZentinelPortal(obj, id="zport", title='Zentinel Portal',
    350                           description='',
    351                           create_userfolder=True,
    352                           email_from_address='postmaster@localhost',
    353                           email_from_name='Portal Administrator',
    354                           validate_email=0, RESPONSE=None):
    355      '''
    356      Adds a portal instance.
    357      '''
    358      gen = PortalGenerator()
    359      from string import strip
    360      id = strip(id)
    361      p = gen.create(obj, id, create_userfolder)
    362      gen.setupDefaultProperties(p, title, description,
    363                                 email_from_address, email_from_name,
    364                                 validate_email)
    365      if RESPONSE is not None:
    366          RESPONSE.redirect(obj.absolute_url()+'/manage_main')
    367