#89 add fasjson support
Merged 3 years ago by bcotton. Opened 3 years ago by ryanlerch.
ryanlerch/elections freeipa-fas  into  master

file modified
+13 -7
@@ -43,6 +43,7 @@ 

  import munch  # noqa

  import six  # noqa

  

+ from fasjson_client import Client

  from fedora.client import AuthError, AppError  # noqa

  from fedora.client.fas2 import AccountSystem  # noqa

  from flask_oidc import OpenIDConnect  # noqa
@@ -64,13 +65,18 @@ 

  

  APP.wsgi_app = fedora_elections.proxy.ReverseProxied(APP.wsgi_app)

  

- # FAS for usernames.

- FAS2 = AccountSystem(

-     APP.config['FAS_BASE_URL'],

-     username=APP.config['FAS_USERNAME'],

-     password=APP.config['FAS_PASSWORD'],

-     insecure=not APP.config['FAS_CHECK_CERT']

- )

+ if APP.config.get('FASJSON'):

+     ACCOUNTS = Client(

+         url=APP.config['FAS_BASE_URL']

+     )

+ else:

+     # FAS for usernames.

+     ACCOUNTS = AccountSystem(

+         APP.config['FAS_BASE_URL'],

+         username=APP.config['FAS_USERNAME'],

+         password=APP.config['FAS_PASSWORD'],

+         insecure=not APP.config['FAS_CHECK_CERT']

+     )

  

  

  # modular imports

file modified
+26 -10
@@ -36,8 +36,9 @@ 

  from fedora_elections import forms

  from fedora_elections import models

  from fedora_elections import (

-     APP, SESSION, FAS2, is_authenticated, is_admin

+     APP, SESSION, ACCOUNTS, is_authenticated, is_admin

  )

+ from fasjson_client.errors import APIError

  

  

  def election_admin_required(f):
@@ -230,9 +231,14 @@ 

          fas_name = None

          if election.candidates_are_fasusers:  # pragma: no cover

              try:

-                 fas_name = FAS2.person_by_username(

-                     form.name.data)['human_name']

-             except (KeyError, AuthError):

+                 if APP.config.get('FASJSON'):

+                     user = ACCOUNTS.get_user(

+                         username=form.name.data).result

+                     fas_name = f'{user['givenname']} {user['surname']}'

There is an equivalent value of human_name for users in LDAP, we just don't make it available. I'll add it to the output model so we can return it directly. I'll call it human_name too to ease transition.

It will be available when https://github.com/fedora-infra/fasjson/pull/98 is merged.

+                 else:

+                     fas_name = ACCOUNTS.person_by_username(

+                         form.name.data)['human_name']

+             except (KeyError, AuthError, APIError):

                  flask.flash(

                      'User `%s` does not have a FAS account.'

                      % form.name.data, 'error')
@@ -286,9 +292,14 @@ 

              fas_name = None

              if election.candidates_are_fasusers:  # pragma: no cover

                  try:

-                     fas_name = FAS2.person_by_username(

-                         candidate[0])['human_name']

-                 except (KeyError, AuthError):

+                     if APP.config.get('FASJSON'):

+                         user = ACCOUNTS.get_user(

+                             username=candidate[0]).result

+                         fas_name = f'{user['givenname']} {user['surname']}'

+                     else:

+                         fas_name = ACCOUNTS.person_by_username(

+                             candidate[0])['human_name']

+                 except (KeyError, AuthError, APIError):

                      SESSION.rollback()

                      flask.flash(

                          'User `%s` does not have a FAS account.'
@@ -356,9 +367,14 @@ 

  

          if election.candidates_are_fasusers:  # pragma: no cover

              try:

-                 candidate.fas_name = FAS2.person_by_username(

-                     candidate.name)['human_name']

-             except (KeyError, AuthError):

+                 if APP.config.get('FASJSON'):

+                     user = ACCOUNTS.get_user(

+                         username=candidate.name).result

+                     candidate.fas_name = f'{user['givenname']} {user['surname']}'

+                 else:

+                     candidate.fas_name = ACCOUNTS.person_by_username(

+                         candidate.name)['human_name']

+             except (KeyError, AuthError, APIError):

                  SESSION.rollback()

                  flask.flash(

                      'User `%s` does not have a FAS account.'

@@ -19,6 +19,8 @@ 

  # You will want to change this for your install

  SECRET_KEY = 'change me'

  

+ FASJSON = False

+ 

  FAS_BASE_URL = 'https://admin.stg.fedoraproject.org/accounts/'

  FAS_USERNAME = ''

  FAS_PASSWORD = ''

file modified
+10 -4
@@ -10,8 +10,9 @@ 

  

  

  from fedora.client import AuthError

- from fedora_elections import SESSION, FAS2, APP

+ from fedora_elections import SESSION, ACCOUNTS, APP

  from fedora_elections.models import Election

+ from fasjson_client.errors import APIError

  

  

  class ElectionForm(FlaskForm):
@@ -155,9 +156,14 @@ 

          if fasusers:  # pragma: no cover

              # We can't cover FAS integration

              try:

-                 title = \

-                     FAS2.person_by_username(candidate.name)['human_name']

-             except (KeyError, AuthError) as err:

+                 if APP.config.get('FASJSON'):

+                     user = ACCOUNTS.get_user(

+                         username=candidate.name).result

+                     title = f'{user['givenname']} {user['surname']}'

+                 else:

+                     title = ACCOUNTS.person_by_username(

+                         candidate.name)['human_name']

+             except (KeyError, AuthError, APIError) as err:

                  APP.logger.debug(err)

          if candidate.url:

              title = '%s <a href="%s" target="_blank" rel="noopener noreferrer">[Info]</a>' % (title, candidate.url)

@@ -15,6 +15,11 @@ 

  ## application, including all elections past, present and future

  FEDORA_ELECTIONS_ADMIN_GROUP = 'elections'

  

+ # Elections directly connects to the accounts backend to get

+ # details of nominees when adding them to an election. 

+ # if FASJSON is false, elections will connect to FAS2. if FASJSON is

+ # True, elections will connect to FASJSON

+ FASJSON = False

  

  ## Fedora-elections can integrate with FAS to retrieve information about the

  ## candidates, the following configuration keys are required for this

Adds the ability to elections to optionally use fasjson / noggin / AAA
replacement instead of FAS2 fir getting usernames

Signed-off-by: Ryan Lerch rlerch@redhat.com

It's either config.get("FASJSON") or config["FASJSON"]. Same thing in other occurences.

rebased onto 4227a56

3 years ago

It's either config.get("FASJSON") or config["FASJSON"]. Same thing in other occurences.

whoops! fixed now

There is an equivalent value of human_name for users in LDAP, we just don't make it available. I'll add it to the output model so we can return it directly. I'll call it human_name too to ease transition.

It will be available when https://github.com/fedora-infra/fasjson/pull/98 is merged.

@bcotton I think this can be merged :)

Pull-Request has been merged by bcotton

3 years ago