Пример #1
0
def callback():
    next_url = url_for('base.index')
    resp = oauth_provider.authorized_response()
    if resp is None or isinstance(resp, OAuthException):
        # FIXME: notify the user, somehow.
        return redirect(next_url)
    session['oauth'] = resp
    Role.create_defaults()
    session['roles'] = [Role.SYSTEM_GUEST, Role.SYSTEM_USER]
    if 'googleapis.com' in oauth_provider.base_url:
        me = oauth_provider.get('userinfo')
        user_id = 'google:%s' % me.data.get('id')
        role = Role.load_or_create(user_id, Role.USER, me.data.get('name'),
                                   email=me.data.get('email'))
    elif 'occrp.org' in oauth_provider.base_url or \
            'investigativedashboard.org' in oauth_provider.base_url:
        me = oauth_provider.get('api/2/accounts/profile/')
        user_id = 'idashboard:user:%s' % me.data.get('id')
        role = Role.load_or_create(user_id, Role.USER,
                                   me.data.get('display_name'),
                                   email=me.data.get('email'),
                                   is_admin=me.data.get('is_admin'))
        for group in me.data.get('groups', []):
            group_id = 'idashboard:%s' % group.get('id')
            Role.load_or_create(group_id, Role.GROUP, group.get('name'))
            session['roles'].append(group_id)
    else:
        raise RuntimeError("Unknown OAuth URL: %r" % oauth_provider.base_url)
    session['roles'].append(user_id)
    session['user'] = role.id
    session['is_admin'] = role.is_admin
    db_session.commit()
    return redirect(next_url)
 def test_roles(self):
     res = self.client.get('/api/roles')
     assert res.json['total'] == 3, res.json
     assert len(res.json['results']) == 3, res.json
     res = Role.load_or_create('foo', Role.USER, 'Foo User')
     session.commit()
     res = self.client.get('/api/roles')
     assert res.json['total'] == 4, res.json
Пример #3
0
 def create_user(self, id='tester', name=None, email=None, is_admin=False):
     role = Role.load_or_create(id,
                                Role.USER,
                                name or id,
                                email=email,
                                is_admin=is_admin)
     session.commit()
     return role
Пример #4
0
 def test_roles(self):
     res = self.client.get('/api/roles')
     assert res.json['total'] == 3, res.json
     assert len(res.json['results']) == 3, res.json
     res = Role.load_or_create('foo', Role.USER, 'Foo User')
     session.commit()
     res = self.client.get('/api/roles')
     assert res.json['total'] == 4, res.json
Пример #5
0
 def create_user(self, id='tester', name=None, email=None, is_admin=False):
     role = Role.load_or_create(id, Role.USER, name or id, email=email,
                                is_admin=is_admin)
     session.commit()
     return role