Page 1 of 1

Rewriting an automatic login

PostPosted: Wed Sep 26, 2012 5:01 am
by Manz
Hey Guys,

I wrote a script to log into a 'visitor' login in version 3.3 but after the upgrade I haven't managed to rewrite it sucessfully. The previous script had, when given a user and password, would log in and create a conn connection to be used by webtest to load images in a certain dataset.

The older script that successfully ran in 3.3

Code: Select all
def getConnectionANDSguestold(request,group):
    import dbconnect
    conn = None
    if dbconnect.IsUsernameaGuest(request.session.get('username',None))=='True':
           logger.info("GUEST user")
           request.session['username']=None
    if request.session.get('username', None):
           logger.debug('attempting to retrieve emdb connection with username:  %s' % request.session.get('username', None))
           conn = getBlitzConnection(request, useragent="OMERO.webtest")
           if not request.session.has_key('processors'):
                  request.session['processors'] = {}
           if conn != None:
                  logger.debug('emdb connection:  %s' % conn._sessionUuid)
    if conn == None:
           # session has timed out. Need to logout and log in again.
           try:
                  _session_logout(request, request.session['server'])
           except:
                  import traceback
                  logger.debug("Failed to log out %s" % traceback.format_exc())
           server_id = request.REQUEST.get('server',None)
           if server_id is not None:
                  blitz = settings.SERVER_LIST.get(pk=int(server_id))
           else:
                  blitz = settings.SERVER_LIST.get(pk=1)
           correspondingGuestDeets=dbconnect.findGuestUser(group)
           logger.debug('attempting to connect emdb with blitz:  %s' % blitz)
           request.session['server'] = blitz.id
           request.session['host'] = blitz.host
           request.session['port'] = blitz.port
           request.session['password'] = correspondingGuestDeets[0][1]
           request.session['username'] = correspondingGuestDeets[0][0]
           request.session['processors'] = {}
           request.session.modified = True
           conn = getBlitzConnection (request, useragent="OMERO.webtest")
           logger.debug('emdb connection: %s server %s' % (conn._sessionUuid, blitz.host))
    return conn



And the new one

Code: Select all
def getConnectionANDSguest(request,group):
    import dbconnect
    from omeroweb.webadmin.forms import LoginForm
    from omeroweb.webclient.decorators import login_required
    from omeroweb.connector import Connector
    from omeroweb.webadmin.webadmin_utils import _checkVersion, _isServerOn, toBoolean
    request.session.modified = True
    error = None
    conn = None
    correspondingGuestDeets=dbconnect.findGuestUser(group)
    logger.info("getConnANDSguest")
    logger.info(correspondingGuestDeets[0][0])
    server_id = request.REQUEST.get('server')
    form = LoginForm(data=request.REQUEST.copy())
    logger.info(form)
    #logger.info(request.REQUEST.copy())
    #if form.is_valid():
    username = correspondingGuestDeets[0][0]
    password = correspondingGuestDeets[0][1]
    server_id = '1'
   # server_id = form.cleaned_data['server']
    #is_secure = toBoolean(form.cleaned_data['ssl'])
    is_secure = 'FALSE'
    logger.info(server_id)
    connector = Connector(server_id, is_secure)
    logger.info(connector)
    # TODO: version check should be done on the low level, see #5983
    #if server_id is not None and username is not None and password is not None \
    #            and _checkVersion(*connector.lookup_host_and_port()):
    conn = connector.create_connection('OMERO.web', username, password)
    return conn



When I run the new one, conn isn't being made, and the following errors are coming up in debug.

Code: Select all
2012-09-26 05:02:56,052 DEBUG [                           omero.gateway] (proc.06009) connect:1684 Creating Session...
2012-09-26 05:02:56,592  INFO [                           omero.gateway] (proc.06009) connect:1714 Failed to create session.
2012-09-26 05:02:56,600 DEBUG [                           omero.gateway] (proc.06009) connect:1715 BlitzGateway.connect().createSession(): Traceback (most recent call last):
  File "/opt/OmeroServerDevel/lib/python/omero/gateway/__init__.py", line 1685, in connect
    self._createSession()
  File "/opt/OmeroServerDevel/lib/python/omero/gateway/__init__.py", line 1579, in _createSession
    self.setSecure(self.secure)
  File "/opt/OmeroServerDevel/lib/python/omero/gateway/__init__.py", line 1549, in setSecure
    if hasattr(self.c, 'createClient') and (secure ^ self.c.isSecure()):
TypeError: unsupported operand type(s) for ^: 'str' and 'bool'

2012-09-26 05:02:56,601 DEBUG [                           omero.gateway] (proc.06009) connect:1731 connect(): Traceback (most recent call last):
  File "/opt/OmeroServerDevel/lib/python/omero/gateway/__init__.py", line 1717, in connect
    self._createSession()
  File "/opt/OmeroServerDevel/lib/python/omero/gateway/__init__.py", line 1569, in _createSession
    self._ic_props[omero.constants.PASSWORD])
  File "/opt/OmeroServerDevel/lib/python/omero/clients.py", line 433, in createSession
    raise omero.ClientError("Session already active. Create a new omero.client or closeSession()")
ClientError: Session already active. Create a new omero.client or closeSession()




Any ideas would be greatly appreciated.

Amanda

Re: Rewriting an automatic login

PostPosted: Wed Sep 26, 2012 8:23 am
by wmoore
Hi,

I seems that the error you're getting is because
Code: Select all
is_secure
should be a boolean, not a string.

Code: Select all
is_secure = 'FALSE'


Try this:
Code: Select all
is_secure = False


However, in 4.4.x OMERO web you shouldn't have to do any of this at all, since the new login_required decorator should handle it all for you. Have a look at http://www.openmicroscopy.org/site/supp ... cData.html

Hope that helps,

Will.

Re: Rewriting an automatic login

PostPosted: Wed Sep 26, 2012 10:26 pm
by Manz
Hey Will

Oh that's brilliant, thanks!

Amanda