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

Source Code for Module ZenModel.ZentinelPortal

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