diff --git a/atst/domain/environment_roles.py b/atst/domain/environment_roles.py index ec34b123..ef8a4b8e 100644 --- a/atst/domain/environment_roles.py +++ b/atst/domain/environment_roles.py @@ -3,9 +3,10 @@ from flask import current_app as app from atst.database import db from atst.models import ( - EnvironmentRole, - ApplicationRole, Environment, + EnvironmentRole, + Application, + ApplicationRole, ApplicationRoleStatus, ) from atst.domain.exceptions import NotFoundError @@ -126,3 +127,15 @@ class EnvironmentRoles(object): .one_or_none() ) return existing_env_role + + @classmethod + def for_user(cls, user_id, portfolio_id): + return ( + db.session.query(EnvironmentRole) + .join(ApplicationRole) + .join(Application) + .filter(Application.portfolio_id == portfolio_id) + .filter(ApplicationRole.application_id == Application.id) + .filter(ApplicationRole.user_id == user_id) + .all() + ) diff --git a/atst/routes/applications/index.py b/atst/routes/applications/index.py index dc9f4c9c..5f0cc5ab 100644 --- a/atst/routes/applications/index.py +++ b/atst/routes/applications/index.py @@ -1,7 +1,8 @@ -from flask import render_template +from flask import render_template, g from .blueprint import applications_bp from atst.domain.authz.decorator import user_can_access_decorator as user_can +from atst.domain.environment_roles import EnvironmentRoles from atst.models.permissions import Permissions @@ -23,4 +24,11 @@ def has_portfolio_applications(_user, portfolio=None, **_kwargs): message="view portfolio applications", ) def portfolio_applications(portfolio_id): - return render_template("applications/index.html") + user_env_roles = EnvironmentRoles.for_user(g.current_user.id, portfolio_id) + environment_access = {} + for env_role in user_env_roles: + environment_access[env_role.environment_id] = env_role.role + + return render_template( + "applications/index.html", environment_access=environment_access + ) diff --git a/templates/applications/index.html b/templates/applications/index.html index 0e765e14..1fd526cf 100644 --- a/templates/applications/index.html +++ b/templates/applications/index.html @@ -52,11 +52,11 @@ heading_tag="h4" ) %} {% for environment in application.environments %} - {{ environment.id }} + {% set env_access = environment_access[environment.id] %}
- {% if g.current_user in environment.users %} + {% if env_access %} {{ environment.displayname }} {{ Icon('link', classes='icon--medium icon--primary') }} @@ -64,9 +64,9 @@ {{ environment.displayname }} {% endif %}
- {% if g.current_user in environment.users %} + {% if env_access %}
- Your env role here! + {{ env_access }}
{% endif %}
diff --git a/tests/domain/test_environment_roles.py b/tests/domain/test_environment_roles.py index b11d14f9..6a5aab2d 100644 --- a/tests/domain/test_environment_roles.py +++ b/tests/domain/test_environment_roles.py @@ -136,3 +136,21 @@ def test_get_for_update(application_role, environment): assert role.application_role == application_role assert role.environment == environment assert role.deleted + + +def test_for_user(application_role, environment): + portfolio = application_role.application.portfolio + user = application_role.user + # create roles for 2 environments + env_role = EnvironmentRoleFactory.create( + application_role=application_role, environment=environment + ) + env_role1 = EnvironmentRoleFactory.create(application_role=application_role) + # create role for environment in a different app in same portfolio + app2 = ApplicationFactory.create(portfolio=portfolio) + app_role2 = ApplicationRoleFactory.create(application=app2, user=user) + env_role2 = EnvironmentRoleFactory.create(application_role=app_role2) + env_roles = EnvironmentRoles.for_user(user.id, portfolio.id) + assert len(env_roles) == 3 + for role in [env_role, env_role1, env_role2]: + assert role in env_roles