Exemplo n.º 1
0
    def _default(self, page=None, *args, **kw):

        page_slug = dict(slug=page)
        hooks.notify('flatpages.before_override_template', args=(page_slug, self))

        page = model.FlatPage.by_slug(page_slug['slug'])

        if page is None:
            abort(404, 'Page not found')


        permission = page.required_permission
        if permission and permission != 'public':
            if permission == 'not_anonymous':
                predicate = predicates.not_anonymous()
            else:
                predicate = predicates.has_permission(permission)

            if not predicate.is_met(request.environ):
                abort(403, 'Forbidden')

        try:
            userid = request.identity['user'].user_id
        except:
            userid = None

        override_template(RootController._default, page.template)

        hooks.notify('flatpages.after_override_template', (page, self))
        return dict(page=page,
                    tg_cache={'expire': self.CACHE_EXPIRE,
                              'key': '%s-%s-%s' % (page.slug, page.updated_at, userid)})
Exemplo n.º 2
0
    def complete(self, email, **kw):
        reg = config['registration_dal'].by_email(email)
        if not reg:
            # flash(_('Registration not found or already activated'))
            return redirect(self.mount_point)

        # Force resolution of lazy property
        reg.activation_link

        registration_config = config.get('_pluggable_registration_config')
        mail_body = registration_config.get(
            'mail_body',
            _('Please click on this link to confirm your registration')
        )
        if '%(activation_link)s' not in mail_body:
            mail_body += '\n \n %(activation_link)s'

        email_data = {'sender': config['registration.email_sender'],
                      'subject': registration_config.get(
                          'mail_subject', _('Please confirm your registration')
                      ),
                      'body': mail_body,
                      'rich': registration_config.get('mail_rich', '')}

        hooks.notify('registration.on_complete', (reg, email_data))

        email_data['body'] = email_data['body'] % reg.dictified
        email_data['rich'] = email_data['rich'] % reg.dictified

        send_email(reg.email_address, **email_data)
        return dict(email=email, email_data=email_data)
Exemplo n.º 3
0
    def activate(self, code, **kw):
        reg = config['registration_dal'].get_inactive(code)
        if not reg:
            flash(_('Registration not found or already activated'), 'error')
            return redirect(self.mount_point)

        u = app_model.User(user_name=reg.user_name,
                           display_name=reg.user_name,
                           email_address=reg.email_address,
                           password=reg.password)

        hooks.notify('registration.before_activation', (reg, u))

        try:
            u = config['registration_dal'].out_of_uow_flush(u)
        except DalIntegrityError:
            flash(_('Username already activated'), 'error')
            return redirect(self.mount_point)

        reg.user_id = instance_primary_key(u)
        reg.password = '******'
        reg.activated = datetime.now()

        hooks.notify('registration.after_activation', (reg, u))

        flash(_('Account succesfully activated'))
        return redirect('/')
Exemplo n.º 4
0
    def _default(self, page=None, *args, **kw):
        page = model.FlatPage.by_slug(page)
        if page is None:
            abort(404, 'Page not found')

        permission = page.required_permission
        if permission and permission != 'public':
            if permission == 'not_anonymous':
                predicate = predicates.not_anonymous()
            else:
                predicate = predicates.has_permission(permission)

            if not predicate.is_met(request.environ):
                abort(403, 'Forbidden')

        try:
            userid = request.identity['user'].user_id
        except:
            userid = None

        override_template(RootController._default, page.template)

        hooks.notify('flatpages.before_render_page', (page, ))

        return dict(page=page,
                    tg_cache={
                        'expire': self.CACHE_EXPIRE,
                        'key':
                        '%s-%s-%s' % (page.slug, page.updated_at, userid)
                    })
Exemplo n.º 5
0
    def complete_registration(cls, registration):
        # Force resolution of lazy property
        registration.activation_link
        registration_config = config.get('_pluggable_registration_config')
        mail_body = registration_config.get(
            'mail_body',
            _('Please click on this link to confirm your registration'))
        if '%(activation_link)s' not in mail_body:
            mail_body += '\n \n %(activation_link)s'

        email_data = {
            'sender':
            config['registration.email_sender'],
            'subject':
            registration_config.get('mail_subject',
                                    _('Please confirm your registration')),
            'body':
            mail_body,
            'rich':
            registration_config.get('mail_rich', '')
        }

        hooks.notify('registration.on_complete', (registration, email_data))

        email_data['body'] = email_data['body'] % registration.dictified
        email_data['rich'] = email_data['rich'] % registration.dictified

        send_email(registration.email_address, **email_data)
        return email_data
Exemplo n.º 6
0
    def submit(self, *args, **kw):
        hooks.notify('registration.before_registration', args=(kw,))

        new_reg = config['registration_dal'].new(**kw)

        hooks.notify('registration.after_registration', args=(new_reg, kw))
        return redirect(url(self.mount_point + '/complete',
                            params=dict(email=new_reg.email_address)))
Exemplo n.º 7
0
 def index(self, *args, **kw):
     hooks.notify('registration.before_registration_form', args=(kw,))
     config['registration_dal'].clear_expired()
     return dict(form=get_form(), value=kw, action=self.mount_point+'/submit')
Exemplo n.º 8
0
    def login(self, token, came_from=None, remember=None):
        try:
            gplusanswer = urlopen('https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=%s' % token)
        except HTTPError as ex:
            log.error(ex.file.read())
            flash(_('Google auth failed, probably token expired'), 'error')
            return redirect('/login')
        google_id = None
        google_token_expiry = None
        google_email = None
        answer = None

        try:
            answer = json.loads(gplusanswer.read().decode('utf-8'))
            if answer['iss'] not in ['accounts.google.com', 'https://accounts.google.com']:
                flash(_("Login error"), "error")
                return redirect_on_fail()
            if not answer['sub']:
                flash(_("Login error"), "error")
                return redirect_on_fail()

            google_id = answer['sub']
            google_token_expiry = datetime.fromtimestamp(int(answer['exp']))

        except Exception:
            flash(_('Fatal error while trying to contact Google'), 'error')
            return redirect_on_fail()
        finally:
            gplusanswer.close()

        ga_user = model.GoogleAuth.ga_user_by_google_id(google_id)

        if ga_user:
            #If the user already exists, just login him.
            login_user(ga_user.user.user_name, remember)

            if has_googletoken_expired(ga_user):
                ga_user.access_token = token
                ga_user.access_token_expiry = google_token_expiry

            hooks.notify('googleplusauth.on_login', args=(answer, ga_user.user))
            redirect_to = add_param_to_query_string(config.sa_auth['post_login_url'], 'came_from', came_from)

            return redirect(redirect_to)

        # User not present
        user_dict = dict(
            user_name='g+:%s' % google_id,
            email_address=answer['email'],
            password=token,
            display_name=answer['name']
        )
        #  Create new user
        hooks.notify('googleplusauth.on_registration', args=(answer, user_dict))
        try:
            u = model.provider.create(app_model.User, user_dict)
        except:
            # clear the session so the user won't be created
            try:  # ming
                model.DBSession.clear()
            except:  # sqlalchemy
                import transaction
                model.DBSession.expunge_all()
                model.DBSession.rollback()
                transaction.abort()
            # query the user so it will be merged
            u = model.provider.query(
                app_model.User,
                filters=dict(email_address=user_dict['email_address']),
            )[1][0]

        #  Create new Google Plus Login User for store data
        gpl = model.GoogleAuth(
            user=u,
            google_id=google_id,
            registered=True,
            just_connected=True,
            access_token=token,
            access_token_expiry=google_token_expiry,
            profile_picture=answer['picture']
        )


        #  Now login and redirect to request page
        login_user(u.user_name, remember)
        redirect_to = add_param_to_query_string(config.sa_auth['post_login_url'], 'came_from', came_from)

        return redirect(redirect_to)