Ejemplo n.º 1
0
def validate_username(form, user_id):

        cleaned_data = form.cleaned_data

        username = cleaned_data.get("username")
        
        # validate username string
        if username:
            if len(username) < 5:
                form._errors["username"] = form.error_class(["'Username' must contain at least 5 characters."])
            elif len(username) >30:
                form._errors["username"] = form.error_class(["'Username' must not exceed 30 characters."])
            elif re.search(INVALID_USERNAME_CHARS, username):
                form._errors["username"] = form.error_class(["'Username' can only contain letters, digits and @/./+/-/_"])
                
            if settings.ESGF_CONFIG:
                # check that the corresponding OpenID is available in the local CoG database
                if user_id is None: # do not check when instance is updated
                    openid = esgfDatabaseManager.buildOpenid(username)
                    
                    if esgfDatabaseManager.checkOpenid(openid):
                        form._errors["username"] = form.error_class(["Username/OpenID already taken in database."])
                        
                    else:
                        # save this openid in the form data so it can be used by the view POST method
                        form.cleaned_data['openid'] = openid      
                        
                        # once the openid is validated, choose the closest possible username
                        _username = createUsername(username)
                        print 'Created username=%s from=%s' % (_username, username)
                        cleaned_data['username'] = _username # override form data
            else:
                # django will automatically check that the username is unique in the CoG database
                pass
Ejemplo n.º 2
0
Archivo: install.py Proyecto: soay/COG
    def _createObjects(self):
        '''Method to populate the database with some initial objects.'''
                
        # Site: reuse default site 'example.com'
        site = Site.objects.get(pk=1)
        logging.info("Updating site: %s" % site)
        site.name = self.siteManager.get('SITE_NAME')
        site.domain = self.siteManager.get('SITE_DOMAIN')
        site.save()
        
        # Test project
        #if not Project.objects.filter(short_name=DEFAULT_PROJECT_SHORT_NAME).exists():
        if Project.objects.count() == 0:
            logging.info("Creating project: %s" % DEFAULT_PROJECT_SHORT_NAME)
            project = Project.objects.create(short_name=DEFAULT_PROJECT_SHORT_NAME, 
                                             long_name='Test Project', 
                                             description='This is a test project',
                                             site=site, active=True)
            initProject(project)
            project.save()
        
        # create Administrator user - one time only
        if User.objects.count() == 0:
            
            # create User object
            logging.info("Creating admin user")
            user = User(first_name='Admin', last_name='User', 
                        username=ROOTADMIN_USERNAME, 
                        email=self.siteManager.get('EMAIL_SENDER', section=SECTION_EMAIL), 
                        is_staff=True, is_superuser=True)
            if settings.ESGF_CONFIG:
                password = self._getRootAdminPassword()
            else:
                password = DEFAULT_ROOTADMIN_PASSWORD
            user.set_password(password)
            user.save()
                        
            # create UserProfile object
            userp = UserProfile(user=user, institution='Institution', city='City', state='State', country='Country',
                                site=site, last_password_update=datetime.datetime.now())
            userp.clearTextPassword=password # needed by esgfDatabaseManager, NOT saved as clear text in any database
            userp.save()
            
            # ESGF database setup
            if settings.ESGF_CONFIG:
                
                # create rootAdmin openid: https://<ESGF_HOSTNAME>/esgf-idp/openid/rootAdmin
                openid = esgfDatabaseManager.buildOpenid(ROOTADMIN_USERNAME)
                UserOpenID.objects.create(user=user, claimed_id=openid, display_id=openid)
                logging.info("Created openid:%s for CoG administrator: %s" % (openid, user.username) )
                
                # insert rootAdmin user in ESGF database
                logging.info("Inserting CoG administrator: %s in ESGF database" % user.username)
                esgfDatabaseManager.insertEsgfUser(user.profile)

            
        # must create and enable 'esgf.idp.peer" as federated CoG peer
        if settings.IDP_REDIRECT is not None and settings.IDP_REDIRECT.strip()  != '':
            idpHostname = settings.IDP_REDIRECT.lower().replace('http://','').replace('https://','')
            try:
                idpPeerSite = PeerSite.objects.get(site__domain=idpHostname)
                idpPeerSite.enabled=True
                idpPeerSite.save()
            except ObjectDoesNotExist:
                site = Site.objects.create(name=idpHostname, domain=idpHostname)
                idpPeerSite = PeerSite.objects.create(site=site, enabled=True)
            print '\tCreated IdP Peer site: %s with enabled=%s' % (idpPeerSite, idpPeerSite.enabled)