WizardTool.py 39.4 KB
Newer Older
Ivan Tyagov's avatar
Ivan Tyagov committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
##############################################################################
#
# Copyright (c) 2006 Nexedi SARL and Contributors. All Rights Reserved.
#                    Romain Courteaud <romain@nexedi.com>
#                    Ivan Tyagov <ivan@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

from AccessControl import ClassSecurityInfo
31
from ZPublisher.HTTPRequest import FileUpload
Ivan Tyagov's avatar
Ivan Tyagov committed
32 33 34 35 36 37 38 39 40
from Globals import InitializeClass, DTMLFile
from Products.ERP5Type.Tool.BaseTool import BaseTool
from Products.ERP5Type import Permissions
from Products.ERP5Wizard import _dtmldir
from Products.CMFCore.utils import getToolByName
from zLOG import LOG, INFO, WARNING, ERROR, DEBUG
from cStringIO import StringIO
from UserDict import UserDict
import xmlrpclib, socket, sys, traceback, urllib, urllib2, base64, cgi
41 42
from AccessControl.SecurityManagement import newSecurityManager, getSecurityManager, setSecurityManager
import zLOG, cookielib
43
from urlparse import urlparse, urlunparse
44 45 46
from base64 import encodestring, decodestring
from urllib import quote, unquote
from DateTime import DateTime
Ivan Tyagov's avatar
Ivan Tyagov committed
47
from Products.PluggableAuthService.interfaces.plugins import IAuthenticationPlugin
48
from Products.ERP5Type.Cache import CachingMethod
49
from urlparse import urlparse
Ivan Tyagov's avatar
Ivan Tyagov committed
50

Ivan Tyagov's avatar
Ivan Tyagov committed
51 52
# global (RAM) cookie storage
cookiejar = cookielib.CookieJar()
53
last_loggedin_user_and_password = None
54
referer  = None
Ivan Tyagov's avatar
Ivan Tyagov committed
55 56 57
installation_status = {'bt5': {'current': 0,
                               'all': 0,},
                       'activity_list': [],}
58

59 60 61
# cookie name to store user's preferred language name
LANGUAGE_COOKIE_NAME = 'configurator_user_preferred_language'

62 63 64 65 66 67 68 69 70 71
def getAvailableLanguageFromHttpAcceptLanguage(http_accept_language,
                                               available_language_list,
                                               default='en'):
  for language_set in http_accept_language.split(','):
    language_tag = language_set.split(';')[0]
    language = language_tag.split('-')[0]
    if language in available_language_list:
      return language
  return default

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
def _isUserAcknowledged(cookiejar):
  """ Is user authenticated to remote system through a cookie. """
  for cookie in cookiejar:
    if cookie.name == '__ac' and cookie.value != '':
      return 1
  return 0
  
def _getAcCookieFromServer(url, opener, cookiejar, username, password, header_dict = {}):
  """ get __ac cookie from server """
  data = urllib.urlencode({'__ac_name':  username,
                           '__ac_password':  password})
  request = urllib2.Request(url, data, header_dict)
  f = opener.open(request)
  return f

Ivan Tyagov's avatar
Ivan Tyagov committed
87 88 89 90
def _setSuperSecurityManager(self, user_name=None):
  """ Change to super user account or passed user_name.
      Return original Security Manager
  """
91
  original_security_manager = getSecurityManager()
Ivan Tyagov's avatar
Ivan Tyagov committed
92 93 94 95 96 97
  if user_name is not None:
    user_folder = self.getPortalObject().acl_users
    user = user_folder.getUserById(user_name).__of__(user_folder)
  else:
    user = self.getWrappedOwner()
  newSecurityManager(self.REQUEST, user)
98
  return original_security_manager
Ivan Tyagov's avatar
Ivan Tyagov committed
99 100 101 102

class GeneratorCall(UserDict):
  """ Class use to generate/interpret XML-RPC call for the wizard. """
  
103 104
  _binary_keys = ("data", "filedata", "previous", "next",)
  _string_keys = ( "command", "server_buffer",)
Ivan Tyagov's avatar
Ivan Tyagov committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

  def __init__(self, *args, **kw):
    UserDict.__init__(self, *args, **kw)
    self.convert_data = {}
    for key in (self._binary_keys + self._string_keys):
      self.setdefault(key, None)
      
  def load(self, xmlrpccall):
    """ Convert the xmlrpccall into the object. """
    self.convert_data = xmlrpclib.loads(xmlrpccall)[0][0]
    for binary_key in self._binary_keys:
      if self.convert_data[binary_key] is not None:
        if isinstance(self.convert_data[binary_key], list):
          self[binary_key] = []
          for item in self.convert_data[binary_key]:
            self[binary_key].append(self._decodeData(item[16:-18]))
        else:
          self[binary_key] = self._decodeData(self.convert_data[binary_key][16:-18])
    ## load string keys 
    for string_key in self._string_keys:
      self[string_key] = self.convert_data[string_key]

  def dump(self):
    """ Dump object to a xmlrpccall. """
    for binary_key in self._binary_keys:
      if isinstance(self[binary_key], list):
        ## we have list of values 
        self.convert_data[binary_key] = []
        for item in self[binary_key]:
          self.convert_data[binary_key].append(self._encodeData(item))
      else:
        if self[binary_key] is not None:
          self.convert_data[binary_key] = self._encodeData(self[binary_key])
        else:
          self.convert_data[binary_key] = None
    for string_key in self._string_keys:
      self.convert_data[string_key] = self[string_key]
    return xmlrpclib.dumps((self.convert_data,), 'GeneratorAnswer', allow_none=1)

  def _decodeData(self, data):
    """ Decode data. """
    binary_decoder = xmlrpclib.Binary()
    binary_decoder.decode(data)
    return binary_decoder.data
Ivan Tyagov's avatar
Ivan Tyagov committed
149

Ivan Tyagov's avatar
Ivan Tyagov committed
150 151 152 153
  def _encodeData(self, data):
    """ Encode data to transmitable text. """
    fp = StringIO()
    try:
154 155
      # data might be ERP5Type.Message.Message instance.
      xmlrpclib.Binary(data=str(data)).encode(fp)
Ivan Tyagov's avatar
Ivan Tyagov committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
      return fp.getvalue()
    finally:
      fp.close()

def getPicklableRequest(REQUEST):
  """ Return 'pickable' request """
  picklable_request = {}
  for key, value in REQUEST.items():
    picklable_request[key] = str(value)
  return picklable_request

def _generateErrorXML(error_message):
  """ Generate HTML for displaying an error. """
  log_message = traceback.format_exc()
  return '<table><tr><td class="error">%s</td></tr></table>' % error_message
Ivan Tyagov's avatar
Ivan Tyagov committed
171

Ivan Tyagov's avatar
Ivan Tyagov committed
172 173 174 175 176 177 178
## server to local preferences id translation table
_server_to_preference_ids_map = {'client_id': 'preferred_express_client_uid',
                                 'current_bc_index': 'preferred_express_erp5_uid',
                                 'password': 'preferred_express_password',
                                 'user_id': 'preferred_express_user_id',}


179
class WizardTool(BaseTool):
Ivan Tyagov's avatar
Ivan Tyagov committed
180
  """ WizardTool is able to generate custom business templates. """
Ivan Tyagov's avatar
Ivan Tyagov committed
181

Ivan Tyagov's avatar
Ivan Tyagov committed
182 183 184 185 186 187 188 189 190
  id = 'portal_wizard'
  meta_type = 'ERP5 Wizard Tool'
  portal_type = 'Wizard Tool'
  isPortalContent = 1 
  isRADContent = 1
  property_sheets = ()
  security = ClassSecurityInfo()
  security.declareProtected(Permissions.ManagePortal, 'manage_overview')
  manage_overview = DTMLFile('explainWizardTool', _dtmldir )
Ivan Tyagov's avatar
Ivan Tyagov committed
191

Ivan Tyagov's avatar
Ivan Tyagov committed
192 193 194 195 196 197 198
  # Stop traversing a concatenated path after the proxy method.
  def __before_publishing_traverse__(self, self2, request):
    path = request['TraversalRequestNameStack']
    if path and path[-1] == 'proxy':
      subpath = path[:-1]
      subpath.reverse()
      request.set('traverse_subpath', subpath)
199 200
      # initialize our root proxy URL which we use for a referer
      global referer
Ivan Tyagov's avatar
Ivan Tyagov committed
201
      path[:-1] = []
202 203 204
      if referer is None:
        referer = '%s/portal_wizard/proxy/%s/view' %(self.getPortalObject().absolute_url(), \
                                                                                   '/'.join(subpath[:3]))      
Ivan Tyagov's avatar
Ivan Tyagov committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

  def _getProxyURL(self, subpath='', query=''):
    # Helper method to construct an URL appropriate for proxying a request.
    # This makes sure that URLs generated by absolute_url at a remote site
    # will be always towards the proxy method again.
    # 
    # Note that the code assumes that VirtualHostBase is visible. The setting
    # of a front-end server must allow this.
    # 
    # This should generate an URL like this:
    # 
    # http://remotehost:9080/VirtualHostBase/http/localhost:8080/VirtualHostRoot/_vh_erp5/_vh_portal_wizard/_vh_proxy/erp5/person_module/2
    part_list = []

    server_url = self.getServerUrl().rstrip('/')
    part_list.append(server_url)

    part_list.append('VirtualHostBase')

    portal_url = self.getPortalObject().absolute_url()
    scheme, rest = urllib.splittype(portal_url)
    addr, path = urllib.splithost(rest)
    host, port = urllib.splitnport(addr, scheme == 'http' and 80 or 443)
    part_list.append(scheme)
    part_list.append('%s:%s' % (host, port))

    part_list.append('VirtualHostRoot')

    method_path = self.absolute_url_path() + '/proxy'
    part_list.extend(('_vh_' + p for p in method_path.split('/') if p))

    server_root = self.getServerRoot().strip('/')

    if isinstance(subpath, (list, tuple)):
      subpath = '/'.join(subpath)

    if not subpath.startswith(server_root):
      part_list.append(server_root)

    part_list.append(subpath)

    url = '/'.join((p for p in part_list if p))
    if query:
      url = url + '?' + query
    return url

  def _getSubsribedUserAndPassword(self):
    """Retrieve the username and password for the subscription from
    the system."""
254 255 256 257 258 259
    user = CachingMethod(self.getExpressConfigurationPreference,  \
                         'WizardTool_preferred_express_user_id', \
                         cache_factory='erp5_content_long')('preferred_express_user_id', '')
    pw = CachingMethod(self.getExpressConfigurationPreference,  \
                       'WizardTool_preferred_express_password', \
                       cache_factory='erp5_content_long')('preferred_express_password', '')
Ivan Tyagov's avatar
Ivan Tyagov committed
260 261 262 263 264 265
    return (user, pw)

  # This is a custom opener director for not handling redirections
  # and errors automatically. This is necessary because the proxy
  # should pass all results to a client as they are.
  simple_opener_director = urllib2.OpenerDirector()
266 267 268
  for name in ('ProxyHandler', 'UnknownHandler', \
               'HTTPHandler', 'FTPHandler', 
               'FileHandler', 'HTTPSHandler',):
Ivan Tyagov's avatar
Ivan Tyagov committed
269 270 271
    handler = getattr(urllib2, name, None)
    if handler is not None:
      simple_opener_director.add_handler(handler())
Ivan Tyagov's avatar
Ivan Tyagov committed
272
  # add cookie support
273
  simple_opener_director.add_handler(urllib2.HTTPCookieProcessor(cookiejar))
Ivan Tyagov's avatar
Ivan Tyagov committed
274
 
Ivan Tyagov's avatar
Ivan Tyagov committed
275 276 277
  security.declareProtected(Permissions.View, 'proxy')
  def proxy(self, **kw):
    """Proxy a request to a server."""
278
    global cookiejar, referer, last_loggedin_user_and_password
Ivan Tyagov's avatar
Ivan Tyagov committed
279 280 281 282 283 284 285 286 287 288 289
    if self.REQUEST['REQUEST_METHOD'] != 'GET':
      # XXX this depends on the internal of HTTPRequest.
      pos = self.REQUEST.stdin.tell()
      self.REQUEST.stdin.seek(0)
      # XXX if filesize is too big, this might cause a problem.
      data = self.REQUEST.stdin.read()
      self.REQUEST.stdin.seek(pos)
    else:
      data = None

    content_type = self.REQUEST.get_header('content-type')
Ivan Tyagov's avatar
Ivan Tyagov committed
290

Ivan Tyagov's avatar
Ivan Tyagov committed
291 292 293 294 295 296 297 298 299 300 301 302
    # XXX if ":method" trick is used, then remove it from subpath.
    if self.REQUEST.traverse_subpath:
      if data is not None:
        user_input = data
      else:
        user_input = self.REQUEST.QUERY_STRING
      if user_input:
        mark = ':method'
        content_type_value = None
        content_type_dict = None
        if content_type:
          content_type_value, content_type_dict = cgi.parse_header(content_type)
Jérome Perrin's avatar
Jérome Perrin committed
303
        if content_type_value == 'multipart/form-data':
Ivan Tyagov's avatar
Ivan Tyagov committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
          fp = StringIO(user_input)
          user_input_dict = cgi.parse_multipart(fp, content_type_dict)
        else:
          user_input_dict = cgi.parse_qs(user_input)

        for i in user_input_dict:
          if i.endswith(mark):
            method_name = i[:-len(mark)]
            method_path = method_name.split('/')
            if self.REQUEST.traverse_subpath[-len(method_path):]==method_path:
              del self.REQUEST.traverse_subpath[-len(method_path):]
              break

    url = self._getProxyURL(self.REQUEST.traverse_subpath,
                            self.REQUEST['QUERY_STRING'])

    # XXX this will send the password unconditionally!
    # I hope https will be good enough.
    header_dict = {}
    user_and_password = self._getSubsribedUserAndPassword()
    if (len(user_and_password)==2 and
        user_and_password[0] and user_and_password[1]):
326 327
      if user_and_password!=last_loggedin_user_and_password:
        # credentials changed we need to renew __ac cookie from server as well
328
	cookiejar.clear() 
329 330 331 332 333 334 335 336 337 338 339 340
      # try login to server only once using cookie method
      if not _isUserAcknowledged(cookiejar):
        server_url = self.getServerUrl()
        f = _getAcCookieFromServer('%s/WebSite_login' %server_url,
                                   self.simple_opener_director,
                                   cookiejar,
                                   user_and_password[0],
                                   user_and_password[1])
        # if server doesn't support cookie authentication try basic authentication
        if not _isUserAcknowledged(cookiejar):
          auth = 'Basic %s' % base64.standard_b64encode('%s:%s' % user_and_password)
          header_dict['Authorization'] = auth
341 342
        # save last credentials we passed to server
	last_loggedin_user_and_password = user_and_password
Ivan Tyagov's avatar
Ivan Tyagov committed
343 344 345
    if content_type:
      header_dict['Content-Type'] = content_type

Ivan Tyagov's avatar
Ivan Tyagov committed
346 347 348 349 350 351
    # send locally saved cookies to remote web server
    if not header_dict.has_key('Cookie'):
      header_dict['Cookie'] = ''
    for cookie in cookiejar:
      # unconditionally send all cookies (no matter if expired or not) as URL is always the same
      header_dict['Cookie']  +=  '%s=%s;' %(cookie.name, cookie.value)
352
    #  include cookies from local browser (like show/hide tabs) which are set directly
Ivan Tyagov's avatar
Ivan Tyagov committed
353
    # by client JavaScript code (i.e. not sent from server)
354 355
    for cookie_name, cookie_value in self.REQUEST.cookies.items():
      header_dict['Cookie']  +=  '%s=%s;' %(cookie_name, cookie_value)
356

357 358
    # add HTTP referer (especially useful in Localizer when changing language)
    header_dict['REFERER'] = self.REQUEST.get('HTTP_REFERER', None) or referer
Ivan Tyagov's avatar
Ivan Tyagov committed
359 360
    request = urllib2.Request(url, data, header_dict)
    f = self.simple_opener_director.open(request)
361
    
Ivan Tyagov's avatar
Ivan Tyagov committed
362 363 364 365
    try:
      data = f.read()
      metadata = f.info()
      response = self.REQUEST.RESPONSE
366
      if f.code> 300 and f.code <400:
367 368 369 370 371 372 373 374 375 376 377
        # adjust return url which my contain proxy URLs as arguments
        location = metadata.getheader('location')
        parsed_url = list(urlparse(location))
        local_site_url_prefix = urllib.quote('%s/portal_wizard/proxy' \
                                              %self.getPortalObject().absolute_url())
        remote_url_parsed = urlparse(self.getServerUrl())
        remote_site_url_prefix = '%s://%s/kb' %(remote_url_parsed[0], remote_url_parsed[1])
        # fix arguments for returned location URL
        parsed_url[4] = parsed_url[4].replace(local_site_url_prefix, remote_site_url_prefix)
        response['location'] = urlunparse(parsed_url)

Ivan Tyagov's avatar
Ivan Tyagov committed
378 379 380
      response.setStatus(f.code, f.msg)
      response.setHeader('content-type', metadata.getheader('content-type'))
      # FIXME this list should be confirmed with the RFC 2616.
381
      for k in ('uri', 'cache-control', 'last-modified',
Ivan Tyagov's avatar
Ivan Tyagov committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
                'etag', 'if-matched', 'if-none-match',
                'if-range', 'content-language', 'content-range'
                'content-location', 'content-md5', 'expires',
                'content-encoding', 'vary', 'pragma', 'content-disposition',
                'content-length', 'age'):
        if k in metadata:
          response.setHeader(k, metadata.getheader(k))
      return data
    finally:
      f.close()

  def _getRemoteWitchTool(self, server_url):
    """ Return remote generator tool interface. """
    server = xmlrpclib.ServerProxy(server_url, allow_none=1)
    witch_tool = server.portal_witch
    return witch_tool

399
  def callRemoteProxyMethod(self, distant_method, server_url=None, use_cache=1, **kw):
Ivan Tyagov's avatar
Ivan Tyagov committed
400
    """ Call proxy method on server. """
401
    configurator_user_preferred_language = self.getConfiguratorUserPreferredLanguage()
402
    def wrapper(distant_method, **kw):
403
      return self._callRemoteMethod(distant_method, use_proxy=1, **kw)['data']
404 405 406 407 408
    if use_cache:
      wrapper = CachingMethod(wrapper,
                              id = 'callRemoteProxyMethod_%s_%s' 
                                     %(distant_method, configurator_user_preferred_language),
                              cache_factory = 'erp5_ui_medium')
409
    rc = wrapper(distant_method, **kw)
410
    return rc
Ivan Tyagov's avatar
Ivan Tyagov committed
411

Ivan Tyagov's avatar
Ivan Tyagov committed
412
  def _callRemoteMethod(self, distant_method, server_url=None, use_proxy=0, **kw):
Ivan Tyagov's avatar
Ivan Tyagov committed
413 414
    """ Call remote method on server and get result. """
    result_call = GeneratorCall()
415
    friendly_server_url = server_url
Ivan Tyagov's avatar
Ivan Tyagov committed
416 417 418
    if server_url is None:
      # calculate it
      server_url = self.getServerUrl() + self.getServerRoot()
419 420 421 422 423 424 425 426
      # include authentication if possible
      user_and_password = self._getSubsribedUserAndPassword()
      if (len(user_and_password)==2 and
          user_and_password[0] and user_and_password[1]):
        friendly_server_url = server_url
        schema = urlparse(server_url)
        server_url = '%s://%s:%s@%s%s' %(schema[0], user_and_password[0], user_and_password[1],
                                         schema[1], schema[2])
Ivan Tyagov's avatar
Ivan Tyagov committed
427
    witch_tool = self._getRemoteWitchTool(server_url)
428 429 430 431 432 433
    parameter_dict = self.REQUEST.form.copy()
    if use_proxy:
      # add remote method arguments
      parameter_dict['method_id'] = distant_method
      parameter_dict['method_kw'] = kw
      distant_method = 'proxyMethodHandler'
Ivan Tyagov's avatar
Ivan Tyagov committed
434
    ## add client arguments
435
    self._updateParameterDictWithServerInfo(parameter_dict)
436 437
    ## handle file upload
    self._updateParameterDictWithFileUpload(parameter_dict)
Ivan Tyagov's avatar
Ivan Tyagov committed
438 439
    ## call remote method 
    try:
Jérome Perrin's avatar
Jérome Perrin committed
440
      method = getattr(witch_tool, distant_method)
Ivan Tyagov's avatar
Ivan Tyagov committed
441 442 443
      html = method(parameter_dict)
    except socket.error, message:
      html = _generateErrorXML("""Cannot contact the server: %s.
444
                                  Please check your network settings."""  %friendly_server_url)
Ivan Tyagov's avatar
Ivan Tyagov committed
445
      zLOG.LOG('Wizard Tool socket error', zLOG.ERROR, message)
Jérome Perrin's avatar
Jérome Perrin committed
446 447 448
      result_call.update({"command": "show",
                          "data": html,
                          "next": None,
Ivan Tyagov's avatar
Ivan Tyagov committed
449 450 451
                          "previous": None})
    except xmlrpclib.ProtocolError, message:
      html = _generateErrorXML("""The server %s refused to reply.
452
                                  Please contact erp5-dev@erp5.org""" %friendly_server_url)
Ivan Tyagov's avatar
Ivan Tyagov committed
453
      zLOG.LOG('Wizard Tool xmlrpc protocol error', zLOG.ERROR, message)
Jérome Perrin's avatar
Jérome Perrin committed
454 455 456
      result_call.update({"command": "show",
                          "data": html,
                          "next": None,
Ivan Tyagov's avatar
Ivan Tyagov committed
457 458
                          "previous": None})
    except xmlrpclib.Fault, message:
459
      html = _generateErrorXML("Error/bug inside the server: %s." %friendly_server_url)
Ivan Tyagov's avatar
Ivan Tyagov committed
460
      zLOG.LOG('Wizard Tool xmlrpc fault', zLOG.ERROR, message)
Jérome Perrin's avatar
Jérome Perrin committed
461 462 463
      result_call.update({"command": "show",
                          "data": html,
                          "next": None,
Ivan Tyagov's avatar
Ivan Tyagov committed
464 465 466 467 468 469 470 471 472
                          "previous": None})
    else:
      result_call.load(html)
      command = result_call["command"]
      html = result_call["data"]
    return result_call

  def _setServerInfo(self, **kw):
    """ Save to local Zope client address info. """
Jérome Perrin's avatar
Jérome Perrin committed
473
    global _server_to_preference_ids_map
Ivan Tyagov's avatar
Ivan Tyagov committed
474 475 476 477 478
    for item, value in kw.items():
      if item in _server_to_preference_ids_map.keys():
        ## save persistently (as preference)
        self.setExpressConfigurationPreference(_server_to_preference_ids_map[item],
                                               value)
Ivan Tyagov's avatar
Ivan Tyagov committed
479

480 481
  def getConfiguratorUserPreferredLanguage(self):
    """ Get configuration language as selected by user """
482
    REQUEST = getattr(self, 'REQUEST', None)
483
    configurator_user_preferred_language = None
484 485
    if REQUEST is not None:
      # language value will be in cookie or REQUEST itself.
486 487 488
      configurator_user_preferred_language = REQUEST.get(LANGUAGE_COOKIE_NAME, None)
      if configurator_user_preferred_language is None:
        # Find a preferred language from HTTP_ACCEPT_LANGUAGE
489 490 491 492
        available_language_list = [i[1] for i in self.WizardTool_getConfigurationLanguageList()]
        configurator_user_preferred_language = getAvailableLanguageFromHttpAcceptLanguage(
          REQUEST.get('HTTP_ACCEPT_LANGUAGE', 'en'),
          available_language_list)
493 494
    if configurator_user_preferred_language is None:
      configurator_user_preferred_language = 'en'
495
    return configurator_user_preferred_language
496

497 498 499 500 501 502 503 504 505
  def _updateParameterDictWithServerInfo(self, parameter_dict):
    """Updates parameter_dict to include local saved server info settings. """
    global _server_to_preference_ids_map
    for key, value in _server_to_preference_ids_map.items():
      parameter_dict[key] = self.getExpressConfigurationPreference(value, None)
    ## add local ERP5 instance url
    parameter_dict['erp5_url'] = self.getPortalObject().absolute_url()
    # add user preffered language
    parameter_dict['user_preferred_language'] = self.getConfiguratorUserPreferredLanguage()
506

507 508 509 510 511 512 513 514 515 516 517
  def _updateParameterDictWithFileUpload(self, parameter_dict):
    """Updates parameter_dict to replace file upload with their file content,
    encoded as XML-RPC Binary
    """
    for key, value in parameter_dict.items():
      if isinstance(value, FileUpload):
        pos = value.tell()
        value.seek(0)
        parameter_dict[key] = xmlrpclib.Binary(value.read())
        value.seek(pos)

Ivan Tyagov's avatar
Ivan Tyagov committed
518 519 520 521 522 523 524 525 526 527 528 529
  def _importBT5FileData(self, bt5_filename, bt5_filedata):
    """ Import bt5 file content. """
    bt5_io = StringIO(bt5_filedata)
    portal_templates =  getToolByName(self.getPortalObject(), 'portal_templates')
    try:
      business_template = portal_templates.importFile(import_file=bt5_io, batch_mode=1)
    except:
      ## importing of generated bt5 failed
      business_template = None
      LOG("Wizard", ERROR, "[FAIL] Import of Nexedi Configurator bt5 file(%s)" %bt5_filename)
      raise
    bt5_io.close()
530
    #install bt5
Ivan Tyagov's avatar
Ivan Tyagov committed
531 532
    portal_workflow =  getToolByName(self.getPortalObject(), 'portal_workflow')
    business_template.install()
Ivan Tyagov's avatar
Ivan Tyagov committed
533

Jérome Perrin's avatar
Jérome Perrin committed
534 535 536 537 538 539 540 541
  security.declareProtected(Permissions.ModifyPortalContent,
                            'installBT5FilesFromServer')
  def installBT5FilesFromServer(self,
                                server_response,
                                execute_after_setup_script=True,
                                install_standard_bt5=True,
                                install_customer_bt5=True,
                                use_super_manager=True):
Ivan Tyagov's avatar
Ivan Tyagov committed
542
    """ Install or update BT5 files which we get from remote server. """
Ivan Tyagov's avatar
Ivan Tyagov committed
543
    global installation_status
Ivan Tyagov's avatar
Ivan Tyagov committed
544 545
    if use_super_manager:
      # set current security manager to owner of site
546
      original_security_manager = _setSuperSecurityManager(self.getPortalObject())
547 548

    portal = self.getPortalObject()
Ivan Tyagov's avatar
Ivan Tyagov committed
549 550
    bt5_files = server_response.get("filedata", [])
    bt5_filenames = server_response["server_buffer"].get("filenames", [])
551
    portal_templates = getToolByName(portal, 'portal_templates')
Ivan Tyagov's avatar
Ivan Tyagov committed
552
    counter = 0
Jérome Perrin's avatar
Jérome Perrin committed
553 554
    LOG("Wizard", INFO,
        "Starting installation for %s" %' '.join(bt5_filenames))
Ivan Tyagov's avatar
Ivan Tyagov committed
555
    installation_status['bt5']['all'] = len(bt5_files)
Ivan Tyagov's avatar
Ivan Tyagov committed
556 557 558 559 560
    #execute_after_setup_script = install_standard_bt5 =  install_customer_bt5 = False # dev mode
    for bt5_id in bt5_filenames:
      if bt5_id.startswith('http://'):
        ## direct download of bt5 files available
        if install_standard_bt5:
561 562
          bt  = portal_templates.download(bt5_id)
          bt.install()
Ivan Tyagov's avatar
Ivan Tyagov committed
563
          installation_status['bt5']['current'] = counter + 1
Jérome Perrin's avatar
Jérome Perrin committed
564
          LOG("Wizard", INFO,
Ivan Tyagov's avatar
Ivan Tyagov committed
565 566 567 568 569 570
              "[OK] standard bt5 installation (HTTP) from %s" %bt5_id)
      else:
        ## remote system supplied file content
        if install_customer_bt5:
          bt5_filedata = bt5_files[counter]
          self._importBT5FileData(bt5_id, bt5_filedata)
Ivan Tyagov's avatar
Ivan Tyagov committed
571
          installation_status['bt5']['current'] = counter + 1
Jérome Perrin's avatar
Jérome Perrin committed
572 573 574
          LOG("Wizard", INFO,
              "[OK] customized bt5 installation (XML-RPC) %s, %s bytes" %
               (bt5_id, len(bt5_filedata)))
Ivan Tyagov's avatar
Ivan Tyagov committed
575 576 577 578 579
      ## ..
      counter += 1
    ## can we execute after setup script that will finish installation on client side?
    bt5_after_setup_script_id = server_response["server_buffer"].get("after_setup_script_id", None)
    if bt5_after_setup_script_id is None and \
Jérome Perrin's avatar
Jérome Perrin committed
580 581
        self.getExpressConfigurationPreference(
                           'preferred_express_configuration_status', False):
Ivan Tyagov's avatar
Ivan Tyagov committed
582
      ## we already have stored after setup script id
Jérome Perrin's avatar
Jérome Perrin committed
583 584
      bt5_after_setup_script_id = self.getExpressConfigurationPreference(
                           'preferred_express_after_setup_script_id', None)
Ivan Tyagov's avatar
Ivan Tyagov committed
585 586 587 588 589 590 591 592 593 594
    if execute_after_setup_script and bt5_after_setup_script_id is not None:
      ## Execute script provided (if) in customer specific business template.
      bt5_customer_template_id = server_response["server_buffer"]['filenames'][-1]
      bt5_customer_template_id = bt5_customer_template_id.replace('.bt5', '')
      after_script = getattr(self, bt5_after_setup_script_id, None)
      if after_script is not None:
        after_script_result = after_script(customer_template_id = bt5_customer_template_id)
        LOG("Wizard", INFO,"[OK] execution of afer setup script %s (for bt5 %s)\n%s"
             %(after_script.getId(), bt5_customer_template_id, after_script_result))
    ## mark this ERP5 instance as configured
Jérome Perrin's avatar
Jérome Perrin committed
595 596 597 598
    self.setExpressConfigurationPreference(
        'preferred_express_configuration_status', 1)
    self.setExpressConfigurationPreference(
        'preferred_express_after_setup_script_id', bt5_after_setup_script_id)
599 600
    # Make sure that the site status is reloaded.
    portal.portal_caches.clearAllCache()
Jérome Perrin's avatar
Jérome Perrin committed
601 602
    LOG("Wizard", INFO,
              "Completed installation for %s" %' '.join(bt5_filenames))
Ivan Tyagov's avatar
Ivan Tyagov committed
603
    if use_super_manager:
604
      setSecurityManager(original_security_manager)
Ivan Tyagov's avatar
Ivan Tyagov committed
605

Ivan Tyagov's avatar
Ivan Tyagov committed
606 607 608
  ######################################################
  ##               Navigation                         ##
  ######################################################
Ivan Tyagov's avatar
Ivan Tyagov committed
609

610
  #security.declareProtected(Permissions.ModifyPortalContent, 'login')
Ivan Tyagov's avatar
Ivan Tyagov committed
611
  def remoteLogin(self, REQUEST):
Ivan Tyagov's avatar
Ivan Tyagov committed
612 613
    """ Login client and show next form. """
    client_id = None
614 615
    user_id = REQUEST.get('field_my_ac_name', None) or self.getExpressConfigurationPreference('preferred_express_user_id')
    REQUEST.form['field_my_ac_name'] =  user_id    
Ivan Tyagov's avatar
Ivan Tyagov committed
616 617 618 619 620 621 622 623 624 625
    password = REQUEST.get('field_my_ac_password', '')
    came_from_method = REQUEST.get('field_my_came_from_method', '')
    ## call remote server
    response = self._callRemoteMethod("getIdentification")
    command = response["command"]
    if command == "show":
      ## server wants some more info - i.e possible 
      ## selection of working business configuration
      if response.get('server_buffer', None) is not None:
        client_id = response['server_buffer'].get('client_id', None)
Jérome Perrin's avatar
Jérome Perrin committed
626 627 628 629
      self._setServerInfo(user_id=user_id,
                          password=password,
                          client_id=client_id)
      return self.WizardTool_dialogForm(form_html=response["data"])
Ivan Tyagov's avatar
Ivan Tyagov committed
630 631
    elif command == "next":
      self._setServerInfo(user_id=user_id, \
632
                          #password=password, \
Ivan Tyagov's avatar
Ivan Tyagov committed
633 634
                          client_id=response['server_buffer'].get('client_id', None), \
                          current_bc_index=response['server_buffer'].get('current_bc_index', None))
635 636 637 638 639 640 641
      # set encoded __ac_express cookie at client's browser
      __ac_express = quote(encodestring(password))
      expires = (DateTime() + 1).toZone('GMT').rfc822()
      REQUEST.RESPONSE.setCookie('__ac_express',
                                 __ac_express,
                                 expires = expires)
      REQUEST.set('__ac_express', __ac_express)
Ivan Tyagov's avatar
Ivan Tyagov committed
642 643 644 645 646 647 648
      return self.next(REQUEST=REQUEST)
    elif command == "login":
      ## invalid user/password
      self.REQUEST.RESPONSE.redirect( \
          'portal_wizard/%s?field_my_ac_name=%s&portal_status_message=%s' \
            %(came_from_method, user_id, response['server_buffer']['message']))
      return 
Ivan Tyagov's avatar
Ivan Tyagov committed
649

Ivan Tyagov's avatar
Ivan Tyagov committed
650 651 652 653 654
  def login(self, REQUEST):
    """ Login client and show next form. """
    user_id = self.getExpressConfigurationPreference('preferred_express_user_id')
    password = REQUEST.get('field_my_ac_password', '')
    if self._isCorrectConfigurationKey(user_id, password):
655 656 657
      # set user preferred configuration language
      user_preferred_language = REQUEST.get('field_my_user_preferred_language', None)
      if user_preferred_language:
658 659 660 661 662
        # Set language value to request so that next page after login
        # can get the value. Because cookie value is available from
        # next request.
        REQUEST.set(LANGUAGE_COOKIE_NAME, user_preferred_language)
        REQUEST.RESPONSE.setCookie(LANGUAGE_COOKIE_NAME,
663
                                   user_preferred_language,
664
                                   path='/',
665
                                   expires=(DateTime()+30).rfc822())
Ivan Tyagov's avatar
Ivan Tyagov committed
666 667 668 669 670 671 672 673 674 675
      # set encoded __ac_express cookie at client's browser
      __ac_express = quote(encodestring(password))
      expires = (DateTime() + 1).toZone('GMT').rfc822()
      REQUEST.RESPONSE.setCookie('__ac_express',
                                 __ac_express,
                                 expires = expires)
      REQUEST.set('__ac_express', __ac_express)
      return self.next(REQUEST=REQUEST)
    else:
      # incorrect user_id / password
676 677
      REQUEST.set('portal_status_message', 
                  self.callRemoteProxyMethod('WizardTool_viewIncorrectConfigurationKeyMessageRenderer'))
Ivan Tyagov's avatar
Ivan Tyagov committed
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
      return self.view()

  def _isCorrectConfigurationKey(self, user_id, password):
    """ Is configuration key correct """
    uf = self.getPortalObject().acl_users
    for plugin_name, plugin in uf._getOb('plugins').listPlugins(IAuthenticationPlugin):
      if plugin.authenticateCredentials({'login':user_id, 
                                                       'password': password}) is not None:
        return 1
    return 0

  def _isUserAllowedAccess(self):
    """ Can user access locally portal_wizard """
    password = self.REQUEST.get('__ac_express', None)
    if password is not None: 
      user_id = self.getExpressConfigurationPreference('preferred_express_user_id')
      password = decodestring(unquote(password))
      return self._isCorrectConfigurationKey(user_id, password)
    return 0

698
  #security.declareProtected(Permissions.ModifyPortalContent, 'next')
Ivan Tyagov's avatar
Ivan Tyagov committed
699 700
  def next(self, REQUEST):
    """ Validate settings and return a new form to the user.  """
Ivan Tyagov's avatar
Ivan Tyagov committed
701 702 703 704
    # check if user is allowed to access service
    if not self._isUserAllowedAccess():
      REQUEST.set('portal_status_message', self.Base_translateString('Incorrect Configuration Key'))
      return self.view()
Ivan Tyagov's avatar
Ivan Tyagov committed
705 706 707 708 709 710 711 712
    response = self._callRemoteMethod("next")
    if isinstance(response['server_buffer'], dict):
      ## Remote server may request us to save some data.
      self._setServerInfo(**response['server_buffer'])
    ## Parse server response
    command = response["command"]
    html = response["data"]
    if command == "show":
Jérome Perrin's avatar
Jérome Perrin committed
713 714
      return self.WizardTool_dialogForm(previous=response['previous'],
                                        form_html=html,
Ivan Tyagov's avatar
Ivan Tyagov committed
715 716 717 718 719 720 721 722
                                        next = response['next'])
    elif command == "update":
      return self.next(REQUEST=REQUEST)
    elif command == "login":
      REQUEST.set('portal_status_message', html)
      return self.view(REQUEST=REQUEST)
    elif command == "install":
      return self.startInstallation(REQUEST=REQUEST)
Ivan Tyagov's avatar
Ivan Tyagov committed
723

724
  #security.declareProtected(Permissions.ModifyPortalContent, 'previous')
Ivan Tyagov's avatar
Ivan Tyagov committed
725 726
  def previous(self, REQUEST):
    """ Display the previous form. """
Ivan Tyagov's avatar
Ivan Tyagov committed
727 728 729 730
    # check if user is allowed to access service
    if not self._isUserAllowedAccess():
      REQUEST.set('portal_status_message', self.Base_translateString('Incorrect Configuration Key'))
      return self.view()
Ivan Tyagov's avatar
Ivan Tyagov committed
731 732 733 734
    response = self._callRemoteMethod('previous')
    command = response["command"]
    html = response['data']
    if command == "show":
Jérome Perrin's avatar
Jérome Perrin committed
735 736 737
      return self.WizardTool_dialogForm(previous=response['previous'],
                                        form_html=html,
                                        next=response['next'])
Ivan Tyagov's avatar
Ivan Tyagov committed
738 739 740
    elif command == "login":
      REQUEST.set('portal_status_message', html)
      return self.view(REQUEST=REQUEST)
Ivan Tyagov's avatar
Ivan Tyagov committed
741

Jérome Perrin's avatar
Jérome Perrin committed
742 743 744 745
  security.declarePublic(Permissions.AccessContentsInformation,
                         'getInstallationStatusReportFromClient')
  def getInstallationStatusReportFromClient(self,
                          active_process_id=None, REQUEST=None):
Ivan Tyagov's avatar
Ivan Tyagov committed
746 747 748 749
    """ Query local ERP5 instance for installation status.
        If installation is over the installation activities and reindexing
        activities should not exists.
    """
Ivan Tyagov's avatar
Ivan Tyagov committed
750
    global installation_status
Ivan Tyagov's avatar
Ivan Tyagov committed
751
    portal_activities = getToolByName(self.getPortalObject(), 'portal_activities')
Ivan Tyagov's avatar
Ivan Tyagov committed
752 753
    is_bt5_installation_over = (portal_activities.countMessageWithTag('initialERP5Setup')==0)
    if 0 == len(portal_activities.getMessageList()) and is_bt5_installation_over:
Ivan Tyagov's avatar
Ivan Tyagov committed
754
      html = self.WizardTool_viewSuccessfulConfigurationMessageRenderer()
Ivan Tyagov's avatar
Ivan Tyagov committed
755
    else:
Ivan Tyagov's avatar
Ivan Tyagov committed
756 757 758 759 760
      if is_bt5_installation_over:
        # only if bt5s are installed start tracking number of activities
        activity_list = portal_activities.getMessageList()
        installation_status['activity_list'].append(len(activity_list))
      html = self.WizardTool_viewRunningInstallationMessage(installation_status = installation_status)
Ivan Tyagov's avatar
Ivan Tyagov committed
761 762
    # set encoding as this is usually called from asynchronous JavaScript call
    self.REQUEST.RESPONSE.setHeader('Content-Type', 'text/html; charset=utf-8');
Ivan Tyagov's avatar
Ivan Tyagov committed
763
    return html
Ivan Tyagov's avatar
Ivan Tyagov committed
764

Ivan Tyagov's avatar
Ivan Tyagov committed
765 766 767 768 769 770
  security.declarePublic(Permissions.AccessContentsInformation, 'getInstallationStatusReportFromServer')
  def getInstallationStatusReportFromServer(self, active_process_id=None, REQUEST=None):
    """ Query remote server (usually only once for some installation status report """
    response = self._callRemoteMethod("getInstallationStatusReport")
    html = response["data"]
    return html
Ivan Tyagov's avatar
Ivan Tyagov committed
771

Ivan Tyagov's avatar
Ivan Tyagov committed
772 773 774 775 776
  security.declareProtected(Permissions.ModifyPortalContent, 'startInstallation')
  def startInstallation(self, REQUEST):
    """ Start installation process as an activity which will query generation server and
       download/install bt5 template files and meanwhile offer user a nice GUI to observe 
       what's happening. """
Ivan Tyagov's avatar
Ivan Tyagov committed
777 778 779 780 781
    global installation_status
    # init installation status
    installation_status['bt5']['all'] = 0
    installation_status['bt5']['current'] = 0
    installation_status['activity_list'] = []
Ivan Tyagov's avatar
Ivan Tyagov committed
782 783
    active_process = self.portal_activities.newActiveProcess()
    REQUEST.set('active_process_id', active_process.getId())
784 785
    request_restore_dict = {'__ac_express': self.REQUEST.get('__ac_express', None),}
    self.activate(active_process=active_process, tag = 'initialERP5Setup').initialERP5Setup(request_restore_dict)
Ivan Tyagov's avatar
Ivan Tyagov committed
786 787 788
    return self.Wizard_viewInstallationStatus(REQUEST)

  security.declareProtected(Permissions.ModifyPortalContent, 'initialERP5Setup')
789
  def initialERP5Setup(self,  request_restore_dict={}):
Ivan Tyagov's avatar
Ivan Tyagov committed
790 791
    """ Get from remote generation server customized bt5 template files 
        and then install them. """
792 793 794 795
    # restore some REQUEST variables as this method is executed in an activity
    # and there's no access to real original REQUEST
    for key, value in request_restore_dict.items():
      self.REQUEST.set(key, value)
Ivan Tyagov's avatar
Ivan Tyagov committed
796 797 798 799 800 801
    self.REQUEST.form['wizard_request_type'] = 'initial_setup'
    # calculate server_url, because after bt5 installation reindexing is started
    # which will make it impossible to get preferences items    
    server_url = self.getServerUrl() + self.getServerRoot()
    server_response = self._callRemoteMethod('getBT5FilesForBusinessConfiguration', server_url)
    ## save erp5_uid which will make it possible to distingush different business conf for client
802 803 804
    current_bc_index = server_response['server_buffer'].get('current_bc_index', None)
    if current_bc_index is not None:
      self._setServerInfo(current_bc_index = current_bc_index)
Ivan Tyagov's avatar
Ivan Tyagov committed
805 806
    self.installBT5FilesFromServer(server_response, True)
    server_response = self._callRemoteMethod('finalizeInstallation', server_url)
Jérome Perrin's avatar
Jérome Perrin committed
807
    LOG("Wizard", INFO,
Ivan Tyagov's avatar
Ivan Tyagov committed
808 809 810 811 812
        "Successfuly installed generated business configuration from %s" %self.getServerUrl())

  security.declareProtected(Permissions.ModifyPortalContent, 'repair')
  def repair(self):
    """ Repair broken ERP5 instance. This will install all business templates 
Jérome Perrin's avatar
Jérome Perrin committed
813
    for ERP5 instance as specified in its business configuration. """
Ivan Tyagov's avatar
Ivan Tyagov committed
814 815 816 817 818 819
    self.REQUEST.form['wizard_request_type'] = 'repair'
    server_response = self._callRemoteMethod('getBT5FilesForBusinessConfiguration')
    if server_response['command'] == "install":
      active_process = self.portal_activities.newActiveProcess()
      self.activate(active_process=active_process).installBT5FilesFromServer(server_response, True)
    html = server_response['data']
Jérome Perrin's avatar
Jérome Perrin committed
820
    LOG("Wizard", INFO,
Ivan Tyagov's avatar
Ivan Tyagov committed
821 822 823 824 825
        "Start repair process for ERP5 instance from %s" %self.getServerUrl())
    return self.WizardTool_dialogForm(form_html = html)

  security.declareProtected(Permissions.ModifyPortalContent, 'update')
  def update(self):
Jérome Perrin's avatar
Jérome Perrin committed
826
    """ Update ERP5's instance standard business templates. """
Ivan Tyagov's avatar
Ivan Tyagov committed
827 828 829 830 831 832 833
    self.REQUEST.form['wizard_request_type'] = 'update'
    server_response = self._callRemoteMethod('getBT5FilesForBusinessConfiguration')
    if server_response['command'] == "install":
      active_process = self.portal_activities.newActiveProcess()
      self.activate(active_process=active_process).installBT5FilesFromServer(server_response,
                                                                             execute_after_setup_script = False)
    html = server_response['data']
Jérome Perrin's avatar
Jérome Perrin committed
834
    LOG("Wizard", INFO,
Ivan Tyagov's avatar
Ivan Tyagov committed
835 836 837 838 839
        "Start update process for ERP5 instance from %s" %self.getServerUrl())
    return self.WizardTool_dialogForm(form_html = html)

  security.declareProtected(Permissions.View, 'getServerUrl')
  def getServerUrl(self):
840 841 842
    return CachingMethod(self.getExpressConfigurationPreference,  \
                         'WizardTool_preferred_witch_tool_server_url', \
                         cache_factory='erp5_content_long')('preferred_witch_tool_server_url', '')
Ivan Tyagov's avatar
Ivan Tyagov committed
843 844 845

  security.declareProtected(Permissions.View, 'getServerRoot')
  def getServerRoot(self):
846 847 848
    return CachingMethod(self.getExpressConfigurationPreference,  \
                         'WizardTool_preferred_witch_tool_server_root', \
                         cache_factory='erp5_content_long')('preferred_witch_tool_server_root', '')
Ivan Tyagov's avatar
Ivan Tyagov committed
849

Ivan Tyagov's avatar
Ivan Tyagov committed
850 851 852
  security.declareProtected(Permissions.View, 'getExpressConfigurationPreference')
  def getExpressConfigurationPreference(self, preference_id, default = None):
    """ Get Express configuration preference """
853
    original_security_manager = _setSuperSecurityManager(self.getPortalObject())
Ivan Tyagov's avatar
Ivan Tyagov committed
854
    portal_preferences = getToolByName(self, 'portal_preferences')
Ivan Tyagov's avatar
Ivan Tyagov committed
855
    preference_value = portal_preferences.getPreference(preference_id, default)
856
    setSecurityManager(original_security_manager)
Ivan Tyagov's avatar
Ivan Tyagov committed
857
    return preference_value
Ivan Tyagov's avatar
Ivan Tyagov committed
858

Ivan Tyagov's avatar
Ivan Tyagov committed
859 860 861 862 863
  security.declareProtected(Permissions.ModifyPortalContent, 'setExpressConfigurationPreference')
  def setExpressConfigurationPreference(self, preference_id, value):
    """ Set Express configuration preference """
    portal_preferences = getToolByName(self, 'portal_preferences')
    if portal_preferences.getActivePreference() is not None:
Jérome Perrin's avatar
Jérome Perrin committed
864
      portal_preferences.setPreference(preference_id, value)