Esempio n. 1
0
    def do_delete(self):
        if not self.is_logged_in:
            self.send_json_error('Not logged in')

        try:
            userid = self.session_user['user_id']
            user = self.auth.store.user_model.get_by_id(userid)

            # delete all piskels for the current user
            piskels = models.get_piskels_for_user(userid)
            for piskel in piskels:
                piskel.delete()
                # force consistency
                db.get(piskel.key())

            # logout current user
            self.auth.unset_session()

            # from webapp2_extras.appengine.auth.models.User
            # http://webapp-improved.appspot.com/_modules/webapp2_extras/appengine/auth/models.html#User
            #
            # def add_auth_id(self, auth_id):
            #   ...
            #   unique = '%s.auth_id:%s' % (self.__class__.__name__, auth_id)
            #   ...
            Unique.delete_multi(
                map(lambda s: 'User.auth_id:' + s, user.auth_ids))

            # delete user entry
            user.key.delete()

            return self.send_json_response({'status': 'ok'})

        except Exception as error:
            return self.send_json_error(repr(error))
Esempio n. 2
0
 def deleteRelationship(self):
     success, existing = Unique.create_multi([
         'AreaUser.uniqueAreaUserString.%s:%s' %
         (self.userKey, self.privateArea)
     ])
     Unique.delete_multi(existing)
     self.key.delete()
Esempio n. 3
0
    def set_email_to_pending(self):
        """ Changes the email address for the current user"""

        new_email = self.pending_change_email.lower()
        success = False
        if new_email != '':
            success, existing = \
                Unique.create_multi(['Member.auth_id:%s'%new_email,
                                     'Member.email_address:%s'%new_email])
            if not success:
                logging.error('Unable to create user for email %s because of \
                    duplicate keys' % new_email)
            else:
                # delete the old unique values
                Unique.delete_multi(['Member.auth_id:%s'%self.email_address,
                                     'Member.email_address:%s'%self.email_address])

                self.email_address=new_email
                self.auth_ids=[new_email]
            self.pending_change_email = ''
            self.put()

        if success:
            return new_email
        else:
            return None
Esempio n. 4
0
 def tx(scope, value):
   key_name = "U%s:%s" % (scope, value,)
   ue = Unique.get_by_key_name(key_name)
   if ue:
     raise UniqueConstraintViolation(scope, value)
   ue = Unique(key_name=key_name)
   ue.put()
Esempio n. 5
0
File: member.py Progetto: bklang/GO2
def forget_member_from_key(the_member_key):
    """ deletes a member, including all gig plans """

    # first find all of the assocs to bands
    the_assocs = assoc.get_assocs_of_member_key(the_member_key=the_member_key,
                                                confirmed_only=False)
    # delete all plans & abdicate as contact for gigs
    for an_assoc in the_assocs:
        plan.delete_plans_for_member_key_for_band_key(the_member_key,
                                                      an_assoc.band)
        gig.reset_gigs_for_contact_key(the_member_key, an_assoc.band)

    # now quit the bands
    the_assoc_keys = [a.key for a in the_assocs]
    ndb.delete_multi(the_assoc_keys)

    # delete the old unique values
    the_member = the_member_key.get()
    if the_member:
        Unique.delete_multi([
            'Member.auth_id:%s' % the_member.email_address,
            'Member.email_address:%s' % the_member.email_address
        ])
    # bye!
    the_member_key.delete()
Esempio n. 6
0
    def post(self):
        user_id = self.request.get('user_id')
        user = User.get_by_auth_id('%s|%s' % (self.module, user_id))
        if user:

            Unique.delete_multi(
                map(lambda s: 'User.auth_id:' + s, user.auth_ids))
            time.sleep(.25)

            user.key.delete()
            AppEventData(event=user_id,
                         event_type='user',
                         transaction='DEL',
                         user=self.user.email).put()
            data_cache.set('events', None)
            data_cache.set('%s-users-tuple' % self.module, None)
            time.sleep(.25)
            return self.render_response('manage_users.html',
                                        success=True,
                                        message='User %s succesfully deleted' %
                                        user_id,
                                        form=forms.AddUserForm(),
                                        users=self.get_users())

        self.redirect('/admin/manage_users')
    def test_login_multiple_matches(self):
        """Earliest created is preferred."""
        email = '*****@*****.**'
        password = '******'

        user1 = User.create(email=email)
        user1.hashed_password = User.hash_password(password)
        user1.put()

        # We'll have to hack around our protections against this sort of thing
        # so we can test what will happen if the protections fail.
        Unique.query().get().key.delete()

        # Create the user we'll login as.
        user2 = User.create(email=email)
        user2.hashed_password = User.hash_password(password)
        user2.put()

        response = self.testapp.post_json('/api/login', {
            'auth_type': 'email',
            'email': email,
            'password': password
        })
        response_user = json.loads(response.body)
        self.assertEqual(response_user['uid'], user1.uid)
Esempio n. 8
0
    def post(self):
        """ Get fields from POST dict """

        if not self.form.validate():
            return self.get()
        username = self.form.username.data.lower()
        name = self.form.name.data.strip()
        last_name = self.form.last_name.data.strip()
        country = self.form.country.data
        tz = self.form.tz.data

        try:
            user_info = self.user_model.get_by_id(long(self.user_id))

            try:
                message = ''
                # update username if it has changed and it isn't already taken
                if username != user_info.username:
                    user_info.unique_properties = ['username', 'email']
                    uniques = [
                        'User.username:%s' % username,
                        'User.auth_id:own:%s' % username,
                    ]
                    # Create the unique username and auth_id.
                    success, existing = Unique.create_multi(uniques)
                    if success:
                        # free old uniques
                        Unique.delete_multi(
                            ['User.username:%s' % user_info.username, 'User.auth_id:own:%s' % user_info.username])
                        # The unique values were created, so we can save the user.
                        user_info.username = username
                        user_info.auth_ids[0] = 'own:%s' % username
                        message += _('Your new username is <strong>{}</strong>').format(username)

                    else:
                        message += _(
                            'The username <strong>{}</strong> is already taken. Please choose another.').format(
                            username)
                        # At least one of the values is not unique.
                        self.add_message(message, 'error')
                        return self.get()
                user_info.name = name
                user_info.last_name = last_name
                user_info.country = country
                user_info.tz = tz
                user_info.put()
                message += " " + _('Thanks, your settings have been saved.')
                self.add_message(message, 'success')
                return self.get()

            except (AttributeError, KeyError, ValueError), e:
                logging.error('Error updating profile: ' + e)
                message = _('Unable to update profile. Please try again later.')
                self.add_message(message, 'error')
                return self.get()

        except (AttributeError, TypeError), e:
            login_error_message = _('Your session has expired.')
            self.add_message(login_error_message, 'error')
            self.redirect_to('login')
Esempio n. 9
0
    def create(klass, **kwargs):
        # Create Unique code, allowing strongly consistent prevention of
        # duplicates, with retries if necessary.

        code = kwargs.pop('code', None)
        if code is None:
            # No code specified. Generate a unique one.
            # Rather than a while, raise an exception after too many tries.
            for num_tries in range(10):
                code = code_phrase.generate()  # default n=2
                is_unique_code = Unique.create(klass.uniqueness_key(code))
                if is_unique_code:
                    break
            if num_tries >= 3:
                logging.error("Running critically low on codes.")
            if code is None:
                raise Exception("Failed to generate unique code.")
        elif not Unique.create(klass.uniqueness_key(code)):
            # Code specified in creation params. Ensure it's unique.
            raise Exception(
                "Could not create with specified code: {}".format(code))

        # Use default from the program config.
        if 'program_label' in kwargs:
            conf = model.Program.get_config(kwargs['program_label'])
            if not kwargs.get('portal_type', None):
                kwargs['portal_type'] = conf.get('default_portal_type', None)
            if not kwargs.get('portal_message', None):
                kwargs['portal_message'] = conf.get('default_portal_message',
                                                    None)

        return super(klass, klass).create(code=code, **kwargs)
Esempio n. 10
0
    def create(klass, check_uniqueness=True, **kwargs):
        """Checks email uniqueness before creating; raises DuplicateUser.

        Checking uniqueness always generates a Unique entity (an entity of
        class 'Unique') that permanently reserves the email address. But
        checking is also optional, and can be passed as False for testing.

        See http://webapp-improved.appspot.com/_modules/webapp2_extras/appengine/auth/models.html#User.create_user
        """

        # If no username, generate one!
        if 'username' not in kwargs:
            kwargs['username'] = User.create_username(**kwargs)

        else:
            if User.is_valid_username(kwargs['username']):
                raise InvalidUsername(
                    "Invalid username {}. Use only letters, numbers, dashes, and underscores."
                    .format(kwargs['username']))

        # Check for uniqueness of email and username
        if check_uniqueness:
            uniqueness_key = 'User.email:' + kwargs['email']
            is_unique_email = Unique.create(uniqueness_key)
            if not is_unique_email:
                raise DuplicateUser(
                    "There is already a user with email {}.".format(
                        kwargs['email']))

            uniqueness_key = 'User.username:'******'username'].lower()
            is_unique_username = Unique.create(uniqueness_key)
            if not is_unique_username:
                # Need to also delete the unused email!
                # To ensure that this works on retry
                is_unique_email.delete()
                raise DuplicateUser(
                    "There is already a user with username {}.".format(
                        kwargs['username']))

        # Register user for the mailchimp list
        should_subscribe = kwargs.pop('should_subscribe', False)
        if should_subscribe is not False:
            fields = {}
            if 'first_name' in kwargs:
                fields['first_name'] = kwargs['first_name']
            if 'last_name' in kwargs:
                fields['last_name'] = kwargs['last_name']
            subscribed = mailchimp.subscribe(kwargs['email'], **fields)
            if subscribed:
                kwargs['receives_updates'] = True

        # Add new user to user full text search index
        kwargs['listed'] = True
        kwargs['created'] = datetime.datetime.now()
        kwargs['modified'] = datetime.datetime.now()
        newUser = super(klass, klass).create(**kwargs)
        index = search.Index(config.user_index)
        index.put(newUser.to_search_document())
        return newUser
Esempio n. 11
0
    def post(self):
        """
              Get fields from POST dict
        """
        if not self.form.validate():
            return self.get()
        username = self.form.username.data.lower()
        name = self.form.name.data.strip()
        last_name = self.form.last_name.data.strip()
        country = self.form.country.data

        try:
            user_info = models.User.get_by_id(long(self.user_id))
            
            try:
                message=''
                # update username if it has changed and it isn't already taken
                if username != user_info.username:
                    user_info.unique_properties = ['username','email']
                    uniques = [
                               'User.username:%s' % username,
                               'User.auth_id:own:%s' % username,
                               ]
                    # Create the unique username and auth_id.
                    success, existing = Unique.create_multi(uniques)
                    if success:
                        # free old uniques
                        Unique.delete_multi(['User.username:%s' % user_info.username, 'User.auth_id:own:%s' % user_info.username])
                        # The unique values were created, so we can save the user.
                        user_info.username=username
                        user_info.auth_ids[0]='own:%s' % username
                        message+= _('Your new username is ') + username + '.'
                        
                    else:
                        message+= _('Username') + ": " + username + " " + _('is already taken. It is not changed.')
                        # At least one of the values is not unique.
                        # Make a list of the property names that failed.
                        props = [name.split(':', 2)[-1] for name in uniques]
                        raise ValueError(_('Properties %r are not unique.' % props))
                user_info.name=name
                user_info.last_name=last_name
                user_info.country=country
                user_info.put()
                message+= " " + _('Your profile has been updated!')
                self.add_message(message,'success')
                return self.get()

            except (AttributeError, KeyError, ValueError), e:
                message = _('Unable to update profile!')
                logging.error('Unable to update profile: ' + e)
                self.add_message(message,'error')
                return self.get()

        except (AttributeError, TypeError), e:
            login_error_message = _('Sorry you are not logged in!')
            self.add_message(login_error_message,'error')
            self.redirect_to('login')
 def post(self):
     email = self.request.get('user_id')
     user_id = ('%s|%s' % (self.module, email))
     user = User.get_by_auth_id(user_id)
     if user:
         Unique.delete_multi( map(lambda s: 'User.auth_id:' + s, user.auth_ids) )
         user.key.delete()
         time.sleep(.25)
     self.redirect('/super_admin/manage_users')
Esempio n. 13
0
 def update_username(self, username):
     if self.username is not None:
         uniqueness_key = 'User.username:'******'username', username)
     uniqueness_key = 'User.username:' + username.lower()
     unique = Unique.create(uniqueness_key)
     if not unique:
         raise DuplicateField(
             "There is already a user with username {}.".format(username))
     return unique
Esempio n. 14
0
 def get(self):
     ageLimit = datetime.datetime.now() - datetime.timedelta(days=1)
     targetUsersQuery = UserModel.query(UserModel.updated < ageLimit)
     targetUsers = targetUsersQuery.iter()
     for targetUser in targetUsers:
         if not targetUser.verified:
             Unique.delete_multi(
                 ["UserModel.auth_id:" + targetUser.auth_ids[0], "UserModel.email:" + targetUser.email])
             targetUser.deleteAllMessages()
             targetUser.getKey()[0].key.delete()
             targetUser.key.delete()
     self.response.write(RESPONSE_OK)
Esempio n. 15
0
 def update(self, handler, newWebsiteURL, newUserAreas, newUserProfession, newUserBio, newEmail, newNotificationFrequency):
     # self.name = newName if newName.strip() != "" else None
     self.websiteURL =  newWebsiteURL if newWebsiteURL.strip() != "" else None
     self.areasOfExpertise = newUserAreas if newUserAreas.strip() != "" else None
     self.currentProfession = newUserProfession if newUserProfession.strip() != "" else None
     self.bio = newUserBio if newUserBio.strip() != "" else None
     self.notificationFrequency = newNotificationFrequency if newNotificationFrequency.strip() != "" else None
     if newEmail != self.email:
         new_auth_id = '%s: %s' % ('email', newEmail)            
         # Make sure that email is unique
         success = Unique.create(new_auth_id)                                
         if success:    
             if (self.emailUser):
                 # Replace the email in the auth ID array
                 new_auth_ids = [x for x in self.auth_ids if not 'email' in x]
                 new_auth_ids.append(new_auth_id)
                 self.auth_ids = new_auth_ids
             # send a validation email 
             WhysaurusUser.send_verification_email(
                 handler, 
                 self.get_id(), 
                 newEmail, 
                 "updating your email address")
             self.email = newEmail if newEmail.strip() != "" else None
             # I should only update index if email is changed, so this is theoretically 
             # the right place, but what if put later fails?
             self.addToSearchIndex()            
         else:
             raise WhysaurusException("The email address %s already exists" % newEmail)                       
     self.put()
Esempio n. 16
0
 def update(self, handler, newWebsiteURL, newUserAreas, newUserProfession, newUserBio, newEmail, newNotificationFrequency):
     # self.name = newName if newName.strip() != "" else None
     self.websiteURL =  newWebsiteURL if newWebsiteURL.strip() != "" else None
     self.areasOfExpertise = newUserAreas if newUserAreas.strip() != "" else None
     self.currentProfession = newUserProfession if newUserProfession.strip() != "" else None
     self.bio = newUserBio if newUserBio.strip() != "" else None
     self.notificationFrequency = newNotificationFrequency if newNotificationFrequency.strip() != "" else None
     if newEmail != self.email:
         new_auth_id = '%s: %s' % ('email', newEmail)
         # Make sure that email is unique
         success = Unique.create(new_auth_id)
         if success:
             if (self.emailUser):
                 # Replace the email in the auth ID array
                 new_auth_ids = [x for x in self.auth_ids if not 'email' in x]
                 new_auth_ids.append(new_auth_id)
                 self.auth_ids = new_auth_ids
             # send a validation email
             WhysaurusUser.send_verification_email(
                 handler,
                 self.get_id(),
                 newEmail,
                 "updating your email address")
             self.email = newEmail if newEmail.strip() != "" else None
             # I should only update index if email is changed, so this is theoretically
             # the right place, but what if put later fails?
             self.addToSearchIndex()
         else:
             raise WhysaurusException("The email address %s already exists" % newEmail)
     self.put()
Esempio n. 17
0
 def rename(self, newUsername, preserveKeys=False):
     if Unique.create("UserModel.auth_id:" + newUsername):
         oldUsername = self.auth_ids[0]
         Unique.delete_multi(["UserModel.auth_id:" + oldUsername])
         oldKey = self.getKey()
         newKey = UserKey(parent=UserKey.newKey(newUsername))
         if preserveKeys:
             newKey.pubkey = oldKey.pubkey
         else:
             self.loginToken = ""
         self.auth_ids[0] = newUsername
         oldKey.key.delete()
         self.put()
         newKey.put()
     else:
         return RESPONSE_USER_NAME_EXISTS
Esempio n. 18
0
def read_feed():
    feedparser._HTMLSanitizer.acceptable_elements = []  # cleans up all html tag
    feeds = feedparser.parse(GOODREADS_RSS_URL)

    for feed in feeds.entries:

        clearer_date = datetime.fromtimestamp(mktime(feed.published_parsed))

        uniques = ['Quote.link.%s' % feed.link,
                   'Quote.date.%s' % clearer_date, ]
        # transactionally create the unique quote based on date and link
        # https://webapp-improved.appspot.com/_modules/webapp2_extras/appengine/auth/models.html#Unique
        success, existing = Unique.create_multi(uniques)

        if success:
            quote = Quote()
            quote.date = clearer_date
            parsed_summary = [a for a in feed.summary.splitlines()
                              if a and a != '-']
            quote.text = parsed_summary[0]
            quote.author = parsed_summary[1]
            quote.link = feed.link
            quote.put()
            logging.info(
                'New feed: {}, dated: {} has been inserted into the datastore'
                .format(feed.link, clearer_date)
            )
        else:
            logging.debug('Properties %r are not unique.' % existing)
Esempio n. 19
0
    def post(self, user_id):
        """ Get fields from POST dict """

        user_data = self._data()
        user_info = models.User.get_by_id(long(user_id))

        logging.info(user_data)

        if not user_info:
            raise Exception("User not found with id: " + user_id)

        username = user_data['username']
        try:
            message = ''
            # update username if it has changed and it isn't already taken

            user_info.unique_properties = ['username', 'email']
            uniques = [
                'User.username:%s' % username,
                'User.auth_id:own:%s' % username,
            ]
            # Create the unique username and auth_id.
            success, existing = Unique.create_multi(uniques)

            if not existing and not success:
                raise Exception("Error creating user")

            # free old uniques
            Unique.delete_multi(
                ['User.username:%s' % user_info.username, 'User.auth_id:own:%s' % user_info.username])
            # The unique values were created, so we can save the user.
            user_info.username = username
            user_info.auth_ids[0] = 'own:%s' % username
            user_info.name = user_data.get('name', None)
            user_info.last_name = user_data.get('last_name', None)

            if user_data.get('password', None) is not None:
                # Password to SHA512
                password = utils.hashing(user_data['password'], self.app.config.get('salt'))
                user_info.password = security.generate_password_hash(password, length=12)

            user_info.put()
            return True
        except (AttributeError, KeyError, ValueError), e:
            logging.error('Error updating profile: ')
            logging.exception(e)
            return False
Esempio n. 20
0
    def oauth2callback(self):
        flow = flow_from_clientsecrets('client_secrets.json',
                               scope=['https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/userinfo.profile'],
                               redirect_uri='http://'+_ConfigDefaults.ksketch_HOSTNAME + '/user/oauth2callback')
        code = self.request.get('code')
        credentials = flow.step2_exchange(code)
        user_info = get_user_info(credentials)

        if user_info:
          # extract some useful fields
          oid = user_info['id']
          email = Crypto.encrypt(user_info['email'])
          try:
              display_name = Crypto.encrypt(user_info['name'])
          except KeyError:
              display_name = Crypto.encrypt(email.partition('@')[0])

            # check if there is a user present with that auth_id
          exist = True
          user = self.auth.store.user_model.get_by_auth_id(oid)
          if not user:
              user = self.auth.store.user_model.get_by_auth_id("https://www.google.com/profiles/"+oid)
          if not user:
              #Starting appver is always 1.0.
              appver = 1.0

              success, user = self.auth.store.user_model.create_user(oid, email=email, display_name=display_name, real_name=display_name, logincount=0,
                                                                          assigned_version=appver, is_admin=False, is_active=True, is_approved=False,
                                                                          birth_month=0, birth_year=0, parent_email="", contact_studies=True,
                                                                          contact_updates=True)
              logging.info('New user created in the DS')

              #update AppUserCount when adding
              AppUserCount.get_and_increment_counter(appver)
              exist = False

          userid = user.get_id()
          if not user.logincount:
              user.logincount = 1
          else:
              user.logincount += 1
          user.lastlogin = datetime.datetime.now()
          user.put()

          token = self.auth.store.user_model.create_auth_token(userid)
          self.auth.get_user_by_token(userid, token)
          logging.info('The user is already present in the DS')
          db.delete(Unique.all())
          self.session.add_flash('You have successfully logged in', 'success')
          if exist:
              if user.is_approved:
                self.redirect('/app/profile.html')
              else:
                self.redirect('/app/register.html')
          else:
              self.redirect('/app/register.html')
        else:
            self.session.add_flash('There was an error while processing the login', 'error')
            self.redirect('/')
Esempio n. 21
0
    def create(klass, **kwargs):
        # Create Unique entity based on email, allowing strongly consistent
        # prevention of duplicates.
        is_unique_email = Unique.create(User.uniqueness_key(kwargs['email']))
        if not is_unique_email:
            raise DuplicateUser(
                "There is already a user with email {}.".format(
                    kwargs['email']))

        return super(klass, klass).create(**kwargs)
Esempio n. 22
0
 def Create(cls, lastname, firstname, gender, email, raw_password, phone):
   uniques = ['%s.%s:%s' % (cls.__name__, 'auth_id', email)]
   ok, existing = Unique.create_multi(uniques)
   if ok:
     password = security.generate_password_hash(raw_password, method='sha1', length=12, pepper=cls.Pepper)
     user = cls(auth_id=email, email=email, lastname=lastname, firstname=firstname, gender=gender, password=password, phone=phone)
     user.put()
     return user
   else:
     return None
Esempio n. 23
0
    def get(self):
        ageLimit = datetime.datetime.now() - datetime.timedelta(days=1)
        targetUsersQuery = UserModel.query(UserModel.updated < ageLimit)
        targetUsers = targetUsersQuery.iter()
        for targetUser in targetUsers:
            if not targetUser.verified:
                try:
                    logging.info("Deleted user: "******"UserModel.auth_id:" + targetUser.auth_ids[0], "UserModel.email:" + targetUser.email])
                    try:
                        targetUser.getKey().key.delete()
                    except IndexError:
                        logging.info("Missing key: " + str(targetUser.auth_ids))
                    targetUser.key.delete()

                except Exception as error:
                    logging.error("Error " + str(type(error)) + " " + str(error) + " " + str(error.message))

        self.response.write(RESPONSE_OK)
Esempio n. 24
0
 def update_email(self, email):
     # Remove old email from unique keys so it can be re-used!
     old_email = self.email
     uniqueness_key = 'User.email:' + old_email
     Unique.delete_multi([uniqueness_key])
     # Update to new email and check uniqueness
     setattr(self, 'email', email)
     uniqueness_key = 'User.email:' + email
     unique = Unique.create(uniqueness_key)
     if not unique:
         raise DuplicateField(
             "There is already a user with email {}.".format(email))
     # Also need to update the user in our mailchimp
     mailchimp.unsubscribe(old_email)
     if self.receives_updates:
         subscribed = mailchimp.subscribe(email)
         # if subscription failed, they might need to resubscribe!
         if not subscribed:
             subscribed = mailchimp.resubscribe(email)
     return unique
Esempio n. 25
0
def forget_member_from_key(the_member_key):
    """ deletes a member, including all gig plans """

    # first find all of the assocs to bands
    the_assocs = assoc.get_assocs_of_member_key(the_member_key=the_member_key, confirmed_only=False)
    # delete all plans & abdicate as contact for gigs
    for an_assoc in the_assocs:
        plan.delete_plans_for_member_key_for_band_key(the_member_key, an_assoc.band)
        gig.reset_gigs_for_contact_key(the_member_key, an_assoc.band)

    # now quit the bands
    the_assoc_keys=[a.key for a in the_assocs]
    ndb.delete_multi(the_assoc_keys)

    # delete the old unique values
    the_member=the_member_key.get()
    if the_member:
        Unique.delete_multi(['Member.auth_id:%s'%the_member.email_address,
                             'Member.email_address:%s'%the_member.email_address])  
    # bye!    
    the_member_key.delete()
 def test_create_code_creates_unique(self):
     """Should be an entry in Unique to ensure unique code."""
     user = User.create(email='*****@*****.**')
     user.put()
     response = self.testapp.post_json(
         '/api/codes',
         {'organization_id': 'triton', 'program_label': 'triton'},
         headers=self.login_headers(user)
     )
     pc = ProjectCohort.query().fetch(1)
     unique = Unique.query().fetch(1)
     self.assertIsNotNone(pc)
     self.assertIsNotNone(unique)
Esempio n. 27
0
	def create(cls, name):
		u"""
		
		@param str name 学校名
		"""
		
		unique = "%s.name.%s" % (cls.__name__, name)
		success, existing = Unique.create(unique)
		
		if success:
			school = cls(name = name)
			return True, school
		else:
			return False, None
Esempio n. 28
0
def generate_unique_phrase(namespace='Phrase', n=2):
    """Use the datastore to create a phrase within a namespace."""
    max_attempts = 10
    phrase = None
    for x in range(max_attempts):
        p = generate_phrase(n=2)
        p_is_unique = Unique.create(namespace + ':' + p)
        if p_is_unique:
            phrase = p
            break
    if phrase is None:
        raise Exception("Could not generate a unique phrase after {} "
                        "attempts.".format(max_attempts))

    return phrase
    def do_delete(self):
        if not self.is_logged_in:
            self.send_json_error('Not logged in')

        try:
            userid = self.session_user['user_id']
            user = self.auth.store.user_model.get_by_id(userid)

            # delete all piskels for the current user
            piskels = models.get_piskels_for_user(userid)
            for piskel in piskels:
                piskel.delete()
                # force consistency
                db.get(piskel.key())

            # logout current user
            self.auth.unset_session()

            # from webapp2_extras.appengine.auth.models.User
            # http://webapp-improved.appspot.com/_modules/webapp2_extras/appengine/auth/models.html#User
            #
            # def add_auth_id(self, auth_id):
            #   ...
            #   unique = '%s.auth_id:%s' % (self.__class__.__name__, auth_id)
            #   ...
            Unique.delete_multi( map(lambda s: 'User.auth_id:' + s, user.auth_ids) )

            # delete user entry
            user.key.delete()

            return self.send_json_response({
                'status': 'ok'
            })

        except Exception as error:
            return self.send_json_error(repr(error))
Esempio n. 30
0
 def post(self):
     emails_to_delete = self.request.get_all('email')
     emails_deleted = []
     applicants_to_delete = []
     applications_to_delete = []
     uniques_to_delete = []
     for email in emails_to_delete:
         applicant, application = query.get_application_by_email(email)
         if applicant:
             applicants_to_delete.append(applicant.key)
             emails_deleted.append(email)
         if application:
             applications_to_delete.append(application.key)
         # Delete auth ids which are just emails
         uniques_to_delete.append('User.auth_id:' + email)
         # Delete unique emails
         uniques_to_delete.append('User.email:' + email)
     ndb.delete_multi(applicants_to_delete + applications_to_delete)
     Unique.delete_multi(uniques_to_delete)
     self.response.write("Deleted:\n")
     self.response.write("\t- %d applicants\n" % len(applicants_to_delete))
     self.response.write("\t- %d applications\n" % len(applications_to_delete))
     self.response.write("\t- %d uniques\n" % len(uniques_to_delete))
     logging.warning("Deleted applicants: %s", ', '.join(emails_deleted))
Esempio n. 31
0
 def Create(cls, lastname, firstname, gender, email, raw_password, phone):
     uniques = ['%s.%s:%s' % (cls.__name__, 'auth_id', email)]
     ok, existing = Unique.create_multi(uniques)
     if ok:
         password = security.generate_password_hash(raw_password,
                                                    method='sha1',
                                                    length=12,
                                                    pepper=cls.Pepper)
         user = cls(auth_id=email,
                    email=email,
                    lastname=lastname,
                    firstname=firstname,
                    gender=gender,
                    password=password,
                    phone=phone)
         user.put()
         return user
     else:
         return None
Esempio n. 32
0
File: member.py Progetto: bklang/GO2
def update_all_uniques():
    the_members = get_all_members(order=False)

    logging.info('starting unique cleanup')
    m_list = []
    t_list = []
    for m in the_members:
        ea = m.email_address.lower()
        if ea != m.email_address:
            # found an upper-case email

            # first, make new auth_id and email_addresses Uniques
            newauth = Unique.create('Member.auth_id:%s' % ea)
            if newauth is False:
                logging.error(
                    'Unable to create unique auth_id for email {0}'.format(ea))

            newemail = Unique.create('Member.email_address:%s' % ea)
            if newemail is False:
                logging.error(
                    'Unable to create unique email_address for email {0}'.
                    format(ea))

            if newauth and newemail:
                # delete the old unique values
                logging.info('deleting old tokens for {0}'.format(
                    m.email_address))
                Unique.delete_multi([
                    'Member.auth_id:%s' % m.email_address,
                    'Member.email_address:%s' % m.email_address
                ])
            else:
                logging.error('did not delete old tokens')

            m.email_address = ea
            m.auth_ids = [ea]
            m_list.append(m)
        else:
            # email address is fine, just make sure we have tokens for this guy
            t_list.append('Member.auth_id:%s' % ea)
            t_list.append('Member.email_address:%s' % ea)

    if m_list:
        ndb.put_multi(m_list)

    if t_list:
        Unique.create_multi(t_list)

    logging.info('unique cleanup done')
Esempio n. 33
0
def update_all_uniques():
    the_members = get_all_members(order=False)

    logging.info('starting unique cleanup')
    m_list=[]
    t_list=[]
    for m in the_members:
        ea = m.email_address.lower()
        if ea != m.email_address:
            # found an upper-case email
        
            # first, make new auth_id and email_addresses Uniques
            newauth = Unique.create('Member.auth_id:%s'%ea)
            if newauth is False:
                logging.error('Unable to create unique auth_id for email {0}'.format(ea))
        
            newemail = Unique.create('Member.email_address:%s'%ea)
            if newemail is False:
                logging.error('Unable to create unique email_address for email {0}'.format(ea))

            if newauth and newemail:            
                # delete the old unique values
                logging.info('deleting old tokens for {0}'.format(m.email_address))
                Unique.delete_multi(['Member.auth_id:%s'%m.email_address,
                                     'Member.email_address:%s'%m.email_address])
            else:
                logging.error('did not delete old tokens')

            m.email_address=ea
            m.auth_ids=[ea]
            m_list.append(m)
        else:
            # email address is fine, just make sure we have tokens for this guy
            t_list.append('Member.auth_id:%s'%ea)
            t_list.append('Member.email_address:%s'%ea)

    if m_list:
        ndb.put_multi(m_list)
        
    if t_list:
        Unique.create_multi(t_list)

    logging.info('unique cleanup done')
Esempio n. 34
0
 def putUnique(self):
     success = Unique.create('AreaUser.uniqueAreaUserString.%s:%s' % (self.userKey, self.privateArea))
     if success:    
         self.put()
Esempio n. 35
0
    def post(self):
        if not self.form.validate():
            self.add_message("There were errors in subbitting the form.",
                             "error")
            return self.get()

        username = self.form.username.data.lower()
        name = self.form.name.data.strip()
        email = self.form.email.data.strip()
        company = self.form.company.data.strip()
        country = self.form.country.data.strip()
        timezone = self.form.timezone.data.strip()

        user_info = User.get_by_id(long(self.user_id))

        try:
            # update username if it has changed and it isn't already taken
            if username != user_info.username:
                user_info.unique_properties = ['username']
                uniques = ['User.username:%s' % username]

                # create the unique username and auth_id
                success, existing = Unique.create_multi(uniques)

                if success:
                    # free old uniques and update user
                    Unique.delete_multi(
                        ['User.username:%s' % user_info.username])
                    user_info.username = username
                    self.add_message(
                        'Your new username is %s.' % format(username),
                        'success')

                else:
                    # username not unique
                    self.add_message(
                        'The username %s is already in use.' %
                        format(username), 'error')
                    return self.get()

            # update email if it has changed and it isn't already taken
            if email != user_info.email:
                user_info.unique_properties = ['email']
                uniques = ['User.email:%s' % email]

                # create the unique username and auth_id
                success, existing = Unique.create_multi(uniques)

                if success:
                    # free old uniques and update user
                    Unique.delete_multi(['User.email:%s' % user_info.email])
                    user_info.email = email
                    self.add_message('Your new email is %s.' % format(email),
                                     'success')

                else:
                    # user's email not unique
                    self.add_message('That email address is already in use.',
                                     'error')
                    return self.get()

            # update database
            user_info.name = name
            user_info.company = company
            user_info.country = country
            user_info.timezone = timezone
            user_info.put()

            self.add_message("Your settings have been saved.", 'success')
            return self.get()

        except (AttributeError, KeyError, ValueError), e:
            logging.error('Error updating profile: ' + e)
            self.add_message(
                'Unable to update profile. Please try again later.', 'error')
            return self.get()
Esempio n. 36
0
	def post(self):
		if not self.form.validate():
			self.add_message("There were errors in subbitting the form.", "error")
			return self.get()

		username = self.form.username.data.lower()
		name = self.form.name.data.strip()
		email = self.form.email.data.strip()
		company = self.form.company.data.strip()
		country = self.form.country.data.strip()
		timezone = self.form.timezone.data.strip()

		user_info = User.get_by_id(long(self.user_id))

		try:
			# update username if it has changed and it isn't already taken
			if username != user_info.username:
				user_info.unique_properties = ['username']
				uniques = ['User.username:%s' % username]
				
				# create the unique username and auth_id
				success, existing = Unique.create_multi(uniques)

				if success:
					# free old uniques and update user
					Unique.delete_multi(['User.username:%s' % user_info.username])
					user_info.username = username
					self.add_message('Your new username is %s.' % format(username), 'success')

				else:
					# username not unique
					self.add_message('The username %s is already in use.' % format(username), 'error')
					return self.get()

			# update email if it has changed and it isn't already taken
			if email != user_info.email:
				user_info.unique_properties = ['email']
				uniques = ['User.email:%s' % email]
				
				# create the unique username and auth_id
				success, existing = Unique.create_multi(uniques)

				if success:
					# free old uniques and update user
					Unique.delete_multi(['User.email:%s' % user_info.email])
					user_info.email = email
					self.add_message('Your new email is %s.' % format(email), 'success')

				else:
					# user's email not unique
					self.add_message('That email address is already in use.', 'error')
					return self.get()

			# update database                
			user_info.name = name
			user_info.company = company
			user_info.country = country
			user_info.timezone = timezone
			user_info.put()

			self.add_message("Your settings have been saved.", 'success')
			return self.get()

		except (AttributeError, KeyError, ValueError), e:
			logging.error('Error updating profile: ' + e)
			self.add_message('Unable to update profile. Please try again later.', 'error')
			return self.get()
Esempio n. 37
0
 def remove_unique_properties(self):
     """Runs on delete to allow email and username to be reused."""
     uniqueness_key_email = 'User.email:' + self.email
     uniqueness_key_username = '******' + self.username
     Unique.delete_multi([uniqueness_key_email, uniqueness_key_username])
Esempio n. 38
0
 def check_username_uniqueness(self, username):
     """Check if username is used"""
     unique = Unique.get_by_id('User.username:' + username.lower())
     return unique is None
Esempio n. 39
0
for np, np_name in new_partners:
  ok, p = Partner.create_user(np, partner_id=np, email=lang.email)
  if not ok:
    p = Partner.get_by_auth_id(np)
  for attr in copy_attrs:
    setattr(p, attr, getattr(lang, attr))
  p.partner_admin = 'lang'
  p.name = np_name
  p.set_password(np+'password')
  p.put()

"""
for np in new_partners:
  p = Partner.get_by_auth_id(np)
  print np, p


uu = ndb.Key(Unique, 'Partner.auth_id:dailynews')
Unique.get_by_id('Partner.auth_id:dailynews')
""""

inf = 'suntimes3.csv'
with open(inf, 'r') as f:
  reps = csv.DictReader(f.readlines())

for rep in reps:
  name = rep['name']
  phone = rep['phone']
  email = rep['email']
  partner_id = rep['partner_id'].lower()
  partner = Partner.get_by_auth_id(partner_id)
Esempio n. 40
0
 def deleteRelationship(self):
     success, existing = Unique.create_multi(['AreaUser.uniqueAreaUserString.%s:%s' % (self.userKey, self.privateArea)])        
     Unique.delete_multi(existing)
     self.key.delete();
     
             
Esempio n. 41
0
 def putUnique(self):
     success = Unique.create('AreaUser.uniqueAreaUserString.%s:%s' %
                             (self.userKey, self.privateArea))
     if success:
         self.put()
Esempio n. 42
0
 def check_email_uniqueness(self, email):
     """Check if email is used"""
     unique = Unique.get_by_id('User.email:' + email.lower())
     return unique is None
Esempio n. 43
0
    def get(self, npid=None):
        # get our request code back from the social login handler above
        code = self.request.get('code')

        # fire up the github auth object
        scope = 'user:email'
        github_helper = github.GithubAuth(scope)

        # retrieve the access token using the code and auth
        try:
            access_token = github_helper.get_access_token(code)
            user_data = github.get_user_info(access_token)
        except:
            message = 'Error while tokening with Github.'
            self.add_message(message, 'error')
            return self.redirect_to('index')

        # see if user is in database
        uid = str(user_data['id'])  # github id
        user_info = User.get_by_uid(uid)

        # less than ideal way to handle excessive 2FA requests
        #if not user_info.activated:
        #	self.add_message("This account has been deactivated due to excessive 2FA requests. Please contact us to resolve.", "error")
        #	return self.redirect_to('about')

        # never seen them, so create user
        if not user_info:
            name = user_data['name']
            username = user_data['login']
            email = user_data['email']
            location = user_data['location']
            company = user_data['company']

            # create entry in db
            user_info = User(last_login=datetime.now(),
                             uid=str(uid),
                             username=username,
                             name=name,
                             company=company,
                             location=location,
                             email=email,
                             activated=True)

            # try to create unique username
            while True:
                user_info.unique_properties = ['username']
                uniques = ['User.username:%s' % user_info.username]
                success, existing = Unique.create_multi(uniques)

                # if we already have that username, create a new one and try again
                if existing:
                    user_info.username = "******" % (username,
                                                   random.randrange(100))
                else:
                    break

            # write out the user
            user_info.put()

            # wait a few seconds for database server to update
            if config.isdev:
                time.sleep(1)  # seriously?

            # send to marketo if we have email
            # if len(email) > 3:
            try:
                email_test = len(email)

            except Exception as ex:
                slack.slack_message(
                    "New user's email appears to be empty: %s." % ex)
                email_test = 0

            if email_test > 3 and not config.isdev:
                try:
                    mc = MarketoClient(config.munchkin_id, config.mclient_id,
                                       config.mclient_secret)
                    try:
                        first = name.split()[0]
                    except:
                        first = ""

                    try:
                        last = name.split()[1]
                    except:
                        last = ""

                    leads = [{
                        "email": email,
                        "firstName": first,
                        "lastName": last,
                        "company": company,
                        "leadSource": config.mclient_leadSource
                    }]
                    lead = mc.execute(
                        method='push_lead',
                        leads=leads,
                        lookupField='email',
                        programName=config.mclient_programName,
                        programStatus=config.mclient_programStatus)
                except Exception as ex:
                    slack.slack_message(
                        "Marketo lead create failed because %s." % ex)

            # slack the new user signup
            slack.slack_message("New user signed up: %s|%s|%s|%s|%s" %
                                (name, username, email, location, company))

        # check out 2FA status
        now_minus_age = datetime.now() + timedelta(0, -config.session_age)

        # load the next destination, if any
        if npid:
            np_info = NextPages.get_by_npid(npid)
            next_page = np_info.url
            print next_page
        else:
            next_page = ""

        # check if 2FA is on
        if user_info.tfenabled and (user_info.last_login < now_minus_age):
            return self.redirect_to('login-tfa',
                                    next=next_page,
                                    uid=user_info.uid)
        else:
            # two factor is disabled, or already complete
            user_info.last_login = datetime.now()
            user_info.put()

        # log the user in
        self.auth.set_session(self.auth.store.user_to_dict(user_info),
                              remember=True)

        # log visit
        log_message = "user logged in"
        log = LogVisit(user=user_info.key,
                       message=log_message,
                       uastring=self.request.user_agent,
                       ip=self.request.remote_addr)
        log.put()
        message = "You have successfully logged in!"

        self.add_message(message, 'success')

        # remove the next page
        if np_info:
            np_info.key.delete()

        # get the destination URL from the next cookie
        if next_page > "":
            return self.redirect(str(next_page))
        else:
            return self.redirect_to('account-dashboard')

        try:
            pass
        except Exception as ex:
            message = "User login went wrong: %s" % ex
            self.add_message(message, 'error')
            return self.redirect_to('index')
Esempio n. 44
0
    def post(self):
        """ Get fields from POST dict """

        if not self.form.validate():
            return self.get()
        username = self.form.username.data.lower()
        name = self.form.name.data.strip()
        last_name = self.form.last_name.data.strip()
        country = self.form.country.data

        try:
            user_info = models.User.get_by_id(long(self.user_id))

            try:
                message = ''
                # update username if it has changed and it isn't already taken
                if username != user_info.username:
                    user_info.unique_properties = ['username', 'email']
                    uniques = [
                        'User.username:%s' % username,
                        'User.auth_id:own:%s' % username,
                    ]
                    # Create the unique username and auth_id.
                    success, existing = Unique.create_multi(uniques)
                    if success:
                        # free old uniques
                        Unique.delete_multi([
                            'User.username:%s' % user_info.username,
                            'User.auth_id:own:%s' % user_info.username
                        ])
                        # The unique values were created, so we can save the user.
                        user_info.username = username
                        user_info.auth_ids[0] = 'own:%s' % username
                        message += _('Your new username is '
                                     ) + '<strong>' + username + '</strong>.'

                    else:
                        message += _(
                            'Username'
                        ) + " <strong>" + username + "</strong> " + _(
                            'is already taken. It is not changed.')
                        # At least one of the values is not unique.
                        self.add_message(message, 'error')
                        return self.get()
                user_info.name = name
                user_info.last_name = last_name
                user_info.country = country
                user_info.put()
                message += " " + _('Your profile has been updated!')
                self.add_message(message, 'success')
                return self.get()

            except (AttributeError, KeyError, ValueError), e:
                message = _('Unable to update profile!')
                logging.error('Unable to update profile: ' + e)
                self.add_message(message, 'error')
                return self.get()

        except (AttributeError, TypeError), e:
            login_error_message = _('Sorry you are not logged in!')
            self.add_message(login_error_message, 'error')
            self.redirect_to('login')
Esempio n. 45
0
    def post(self):
        """ Get fields from POST dict """

        if not self.form.validate():
            return self.get()
        username = self.form.username.data.lower()
        name = self.form.name.data.strip()
        last_name = self.form.last_name.data.strip()
        country = self.form.country.data.strip()
        bio = self.form.bio.data.strip()
        twitter_widget_id = self.form.twitter_widget_id.data.strip()
        gravatar_url = self.form.gravatar_url.data.strip()
        google_plus_profile = self.form.google_plus_profile.data.strip()

        try:
            user_info = models.User.get_by_id(long(self.user_id))

            try:
                message = ''
                # update username if it has changed and it isn't already taken
                if username != user_info.username:
                    user_info.unique_properties = ['username', 'email']
                    uniques = [
                        'User.username:%s' % username,
                        'User.auth_id:own:%s' % username,
                    ]
                    # Create the unique username and auth_id.
                    success, existing = Unique.create_multi(uniques)
                    if success:
                        # free old uniques
                        Unique.delete_multi([
                            'User.username:%s' % user_info.username,
                            'User.auth_id:own:%s' % user_info.username
                        ])
                        # The unique values were created, so we can save the user.
                        user_info.username = username
                        user_info.auth_ids[0] = 'own:%s' % username
                        message += _(
                            'Your new username is %s' %
                            '<strong>{0:>s}</strong>'.format(username))

                    else:
                        message += _(
                            'The username %s is already taken. Please choose another.'
                            % '<strong>{0:>s}</strong>'.format(username))
                        # At least one of the values is not unique.
                        self.add_message(message, 'error')
                        return self.get()
                user_info.name = name
                user_info.last_name = last_name
                user_info.country = country
                user_info.bio = bio
                user_info.twitter_widget_id = twitter_widget_id
                user_info.gravatar_url = gravatar_url
                user_info.google_plus_profile = google_plus_profile
                user_info.put()
                message += " " + _(
                    'Thanks, your settings have been saved.  You may now dance.'
                )
                self.add_message(message, 'success')
                return self.get()

            except (AttributeError, KeyError, ValueError), e:
                logging.error('Error updating profile: ' + e)
                message = _(
                    'Unable to update profile. Please try again later.')
                self.add_message(message, 'error')
                return self.get()

        except (AttributeError, TypeError), e:
            login_error_message = _('Sorry you are not logged in.')
            self.add_message(login_error_message, 'error')
            self.redirect_to('login')
Esempio n. 46
0
	def get(self):
		# get info from Google login
		current_user = users.get_current_user()

		# handle old and new users
		try:
			uid = current_user.user_id()

			# see if user is in database
			user_info = User.get_by_uid(uid)

			# get the destination URL from the next parameter
			next = self.request.get('next')

			# create association if user doesn't exist
			if user_info is None:
				username = current_user.email().split("@")[0]
				email = current_user.email()

				# create entry in db
				user_info = User(
					last_login = datetime.now(),
					uid = str(uid),
					username = username,
					email = email,
					activated = True
				)

				# try to create unique username
				while True:
					user_info.unique_properties = ['username']
					uniques = ['User.username:%s' % user_info.username]
					success, existing = Unique.create_multi(uniques)

					# if we already have that username, create a new one and try again
					if existing:
						user_info.username = "******" % (username, random.randrange(100)) 
					else:
						break
				
				# write out the user
				user_info.put()

				# wait a few seconds for database server to update
				time.sleep(1)
				log_message = "new user registered"

				# slack the new user signup
				if config.debug:
					in_dev = " (in development)"
				else:
					in_dev = ""

				slack_data = {
					'text': "Woot! New user %s just signed up%s!" % (user_info.username, in_dev),
					'username': "******",
					'icon_emoji': ":cloud:" 
				}
				h = httplib2.Http()
				resp, content = h.request(config.slack_webhook, 
			        'POST', 
			        json.dumps(slack_data),
			        headers={'Content-Type': 'application/json'})

			else:
				# existing user logging in - force a2fa check before continuing
				now_minus_an_hour = datetime.now() + timedelta(0, -config.session_age)

				if user_info.tfenabled and (user_info.last_login < now_minus_an_hour): 
						return self.redirect_to('login-tfa', next=next)
				else:
					# two factor is disabled, or already complete
					user_info.last_login = datetime.now()
					user_info.put()
					log_message = "user login"

			# set the user's session
			self.auth.set_session(self.auth.store.user_to_dict(user_info), remember=True)
			
			# log visit
			log = LogVisit(
				user = user_info.key,
				message = log_message,
				uastring = self.request.user_agent,
				ip = self.request.remote_addr
			)
			log.put()
			message = "You have successfully logged in!"            
			self.add_message(message, 'success')

			# take user to whatever page was originally requested, or status if none
			if next:
				return self.redirect(str(next))
			else:
				return self.redirect_to('account-status')
				
		except Exception as ex:
			message = "No user authentication information received from Google: %s" % ex            
			self.add_message(message, 'error')
			return self.redirect_to('home')
Esempio n. 47
0
 def email_exists(klass, email):
     """Test if this email has been registered, idempotent."""
     return Unique.get_by_id(User.uniqueness_key(email)) is not None
Esempio n. 48
0
    def get(self):
        # get info from Google login
        current_user = users.get_current_user()

        # handle old and new users
        try:
            uid = current_user.user_id()

            # see if user is in database
            user_info = User.get_by_uid(uid)

            # get the destination URL from the next parameter
            next = self.request.get('next')

            # create association if user doesn't exist
            if user_info is None:
                username = current_user.email().split("@")[0]
                email = current_user.email()

                # create entry in db
                user_info = User(last_login=datetime.now(),
                                 uid=str(uid),
                                 username=username,
                                 email=email,
                                 activated=True)

                # try to create unique username
                while True:
                    user_info.unique_properties = ['username']
                    uniques = ['User.username:%s' % user_info.username]
                    success, existing = Unique.create_multi(uniques)

                    # if we already have that username, create a new one and try again
                    if existing:
                        user_info.username = "******" % (username,
                                                       random.randrange(100))
                    else:
                        break

                # write out the user
                user_info.put()

                # wait a few seconds for database server to update
                time.sleep(1)
                log_message = "new user registered"

                # slack the new user signup
                if config.debug:
                    in_dev = " (in development)"
                else:
                    in_dev = ""

                slack_data = {
                    'text':
                    "Woot! New user %s just signed up%s!" %
                    (user_info.username, in_dev),
                    'username':
                    "******",
                    'icon_emoji':
                    ":cloud:"
                }
                h = httplib2.Http()
                resp, content = h.request(
                    config.slack_webhook,
                    'POST',
                    json.dumps(slack_data),
                    headers={'Content-Type': 'application/json'})

            else:
                # existing user logging in - force a2fa check before continuing
                now_minus_an_hour = datetime.now() + timedelta(
                    0, -config.session_age)

                if user_info.tfenabled and (user_info.last_login <
                                            now_minus_an_hour):
                    return self.redirect_to('login-tfa', next=next)
                else:
                    # two factor is disabled, or already complete
                    user_info.last_login = datetime.now()
                    user_info.put()
                    log_message = "user login"

            # set the user's session
            self.auth.set_session(self.auth.store.user_to_dict(user_info),
                                  remember=True)

            # log visit
            log = LogVisit(user=user_info.key,
                           message=log_message,
                           uastring=self.request.user_agent,
                           ip=self.request.remote_addr)
            log.put()
            message = "You have successfully logged in!"
            self.add_message(message, 'success')

            # take user to whatever page was originally requested, or status if none
            if next:
                return self.redirect(str(next))
            else:
                return self.redirect_to('account-status')

        except Exception as ex:
            message = "No user authentication information received from Google: %s" % ex
            self.add_message(message, 'error')
            return self.redirect_to('home')
Esempio n. 49
0
    def post(self):
        """ Get fields from POST dict """

        if not self.form.validate():
            return self.get()
        username = self.form.username.data.lower()
        name = self.form.name.data.strip()
        last_name = self.form.last_name.data.strip()
        country = self.form.country.data.strip()
        bio = self.form.bio.data.strip()
        twitter_widget_id = self.form.twitter_widget_id.data.strip()
        gravatar_url = self.form.gravatar_url.data.strip()
        google_plus_profile = self.form.google_plus_profile.data.strip()
        
        try:
            user_info = models.User.get_by_id(long(self.user_id))

            try:
                message=''
                # update username if it has changed and it isn't already taken
                if username != user_info.username:
                    user_info.unique_properties = ['username','email']
                    uniques = [
                               'User.username:%s' % username,
                               'User.auth_id:own:%s' % username,
                               ]
                    # Create the unique username and auth_id.
                    success, existing = Unique.create_multi(uniques)
                    if success:
                        # free old uniques
                        Unique.delete_multi(['User.username:%s' % user_info.username, 'User.auth_id:own:%s' % user_info.username])
                        # The unique values were created, so we can save the user.
                        user_info.username=username
                        user_info.auth_ids[0]='own:%s' % username
                        message+= _('Your new username is %s' % '<strong>{0:>s}</strong>'.format(username) )

                    else:
                        message+= _('The username %s is already taken. Please choose another.'
                                % '<strong>{0:>s}</strong>'.format(username) )
                        # At least one of the values is not unique.
                        self.add_message(message, 'error')
                        return self.get()
                user_info.name=name
                user_info.last_name=last_name
                user_info.country=country
                user_info.bio=bio
                user_info.twitter_widget_id=twitter_widget_id
                user_info.gravatar_url=gravatar_url
                user_info.google_plus_profile=google_plus_profile
                user_info.put()
                message+= " " + _('Thanks, your settings have been saved.  You may now dance.')
                self.add_message(message, 'success')
                return self.get()

            except (AttributeError, KeyError, ValueError), e:
                logging.error('Error updating profile: ' + e)
                message = _('Unable to update profile. Please try again later.')
                self.add_message(message, 'error')
                return self.get()

        except (AttributeError, TypeError), e:
            login_error_message = _('Sorry you are not logged in.')
            self.add_message(login_error_message, 'error')
            self.redirect_to('login')