Skip to content

Commit

Permalink
Merge branch 'Simon-Initiative:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
dtiwarATS authored Feb 28, 2024
2 parents ecca26e + bcc7735 commit 1a67d33
Show file tree
Hide file tree
Showing 67 changed files with 1,106 additions and 469 deletions.
6 changes: 2 additions & 4 deletions assets/src/apps/delivery/layouts/deck/DeckLayoutFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import {
selectCurrentActivityTreeAttemptState,
} from '../../store/features/groups/selectors/deck';
import {
selectEnableHistory,
selectIsLegacyTheme,
selectPageContent,
selectPreviewMode,
Expand Down Expand Up @@ -229,7 +228,6 @@ const DeckLayoutFooter: React.FC = () => {
const isGoodFeedback = useSelector(selectIsGoodFeedback);
const currentFeedbacks = useSelector(selectCurrentFeedbacks);
const nextActivityId: string = useSelector(selectNextActivityId);
const enableHistory = useSelector(selectEnableHistory);
const lastCheckTimestamp = useSelector(selectLastCheckTriggered);
const lastCheckResults = useSelector(selectLastCheckResults);
const initPhaseComplete = useSelector(selectInitPhaseComplete);
Expand Down Expand Up @@ -613,13 +611,13 @@ const DeckLayoutFooter: React.FC = () => {
if (activityHistoryTimeStamp === 0) {
updateActivityHistoryTimeStamp();
}
//** there are cases when wrong trap state gets trigger but user is still allowed to jump to another activity */
//** there are cases when wrong trap state gets trigger but user is still allowed to jump to another activity */
//** if we don't do this then, every time Next button will trigger a check events instead of navigating user to respective activity */
dispatch(
nextActivityId === 'next' ? navigateToNextActivity() : navigateToActivity(nextActivityId),
);
dispatch(setNextActivityId({ nextActivityId: '' }));
} else if (!enableHistory) {
} else if (!currentActivity?.custom?.showCheckBtn) {
dispatch(triggerCheck({ activityId: currentActivity?.id }));
} else {
dispatch(setIsGoodFeedback({ isGoodFeedback: false }));
Expand Down
6 changes: 3 additions & 3 deletions assets/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11428,9 +11428,9 @@ invariant@^2.2.1, invariant@^2.2.4:
loose-envify "^1.0.0"

ip@^2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/ip/-/ip-2.0.0.tgz"
integrity sha512-WKa+XuLG1A1R0UWhl2+1XQSi+fZWMsYKffMZTTYsiZaUD8k2yDAj5atimTUD2TZkyCkNEeYE5NhFZmupOGtjYQ==
version "2.0.1"
resolved "https://registry.yarnpkg.com/ip/-/ip-2.0.1.tgz#e8f3595d33a3ea66490204234b77636965307105"
integrity sha512-lJUL9imLTNi1ZfXT+DU6rBBdbiKGBuay9B6xGSPVjUeQwaH1RIGqef8RZkUtHioLmSNpPR5M4HVKJGm1j8FWVQ==

[email protected]:
version "1.9.1"
Expand Down
111 changes: 75 additions & 36 deletions lib/oli/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -440,14 +440,61 @@ defmodule Oli.Accounts do
Repo.exists?(query)
end

def at_least_content_admin?(%Author{system_role_id: system_role_id}) do
SystemRole.role_id().content_admin == system_role_id or
SystemRole.role_id().account_admin == system_role_id or
SystemRole.role_id().system_admin == system_role_id
end

def at_least_content_admin?(_), do: false

def at_least_account_admin?(%Author{system_role_id: system_role_id}) do
SystemRole.role_id().account_admin == system_role_id or
SystemRole.role_id().system_admin == system_role_id
end

def at_least_account_admin?(_), do: false

@doc """
Returns true if an author is a content admin.
"""
def is_content_admin?(%Author{system_role_id: system_role_id}) do
SystemRole.role_id().content_admin == system_role_id
end

def is_content_admin?(_), do: false

@doc """
Returns true if an author is an account admin.
"""
def is_account_admin?(%Author{system_role_id: system_role_id}) do
SystemRole.role_id().account_admin == system_role_id
end

def is_account_admin?(_), do: false

@doc """
Returns true if an author is an administrator.
Returns true if an author is a system admin.
"""
def is_admin?(%Author{system_role_id: system_role_id}) do
SystemRole.role_id().admin == system_role_id
def is_system_admin?(%Author{system_role_id: system_role_id}) do
SystemRole.role_id().system_admin == system_role_id
end

def is_admin?(_), do: false
def is_system_admin?(_), do: false

@doc """
Returns true if an author has some role admin.
"""

def has_admin_role?(%Author{system_role_id: system_role_id}) do
system_role_id in [
SystemRole.role_id().system_admin,
SystemRole.role_id().account_admin,
SystemRole.role_id().content_admin
]
end

def has_admin_role?(_), do: false

@doc """
Returns an author if one matches given email, or creates and returns a new author
Expand Down Expand Up @@ -721,48 +768,40 @@ defmodule Oli.Accounts do
end

def can_access?(author, project) do
admin_role_id = SystemRole.role_id().admin

case author do
if has_admin_role?(author) do
# Admin authors have access to every project
%{system_role_id: ^admin_role_id} ->
true

true
else
# querying join table rather than author's project associations list
# in case the author has many projects
_ ->
Repo.one(
from(assoc in "authors_projects",
where:
assoc.author_id == ^author.id and
assoc.project_id == ^project.id,
select: count(assoc)
)
) != 0
Repo.one(
from(assoc in "authors_projects",
where:
assoc.author_id == ^author.id and
assoc.project_id == ^project.id,
select: count(assoc)
)
) != 0
end
end

def can_access_via_slug?(author, project_slug) do
admin_role_id = SystemRole.role_id().admin

case author do
if has_admin_role?(author) do
# Admin authors have access to every project
%{system_role_id: ^admin_role_id} ->
true

true
else
# querying join table rather than author's project associations list
# in case the author has many projects
_ ->
Repo.one(
from(assoc in "authors_projects",
join: p in "projects",
on: assoc.project_id == p.id,
where:
assoc.author_id == ^author.id and
p.slug == ^project_slug,
select: count(assoc)
)
) != 0
Repo.one(
from(assoc in "authors_projects",
join: p in "projects",
on: assoc.project_id == p.id,
where:
assoc.author_id == ^author.id and
p.slug == ^project_slug,
select: count(assoc)
)
) != 0
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/oli/accounts/schemas/system_role.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ defmodule Oli.Accounts.SystemRole do
def role_id,
do: %{
author: 1,
admin: 2
system_admin: 2,
account_admin: 3,
content_admin: 4
}

schema "system_roles" do
Expand Down
26 changes: 8 additions & 18 deletions lib/oli/authoring/course.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Oli.Authoring.Course do
import Ecto.Query, warn: false

alias Oli.Accounts.{SystemRole, Author}
alias Oli.Accounts.Author
alias Oli.Authoring.Authors.AuthorProject
alias Oli.Authoring.{Collaborators, ProjectSearch}
alias Oli.Authoring.Course.{Project, Family, ProjectResource, ProjectAttributes}
Expand All @@ -10,7 +10,7 @@ defmodule Oli.Authoring.Course do
alias Oli.Publishing
alias Oli.Publishing.Publications.Publication
alias Oli.Publishing.PublishedResource
alias Oli.Repo
alias Oli.{Accounts, Repo}
alias Oli.Repo.{Paging, Sorting}
alias Oli.Resources.{ResourceType, Revision, ScoringStrategy}

Expand Down Expand Up @@ -76,13 +76,9 @@ defmodule Oli.Authoring.Course do
end

def get_projects_for_author(author) do
admin_role_id = SystemRole.role_id().admin

case author do
# Admin authors have access to every project
%{system_role_id: ^admin_role_id} -> Repo.all(Project)
_ -> Repo.preload(author, [:projects]).projects
end
if Accounts.has_admin_role?(author),
do: Repo.all(Project),
else: Repo.preload(author, [:projects]).projects
end

def browse_projects(
Expand All @@ -91,19 +87,13 @@ defmodule Oli.Authoring.Course do
%Sorting{} = sorting,
opts \\ []
) do
admin_role_id = SystemRole.role_id().admin
include_deleted = Keyword.get(opts, :include_deleted, false)
admin_show_all = Keyword.get(opts, :admin_show_all, true)
text_search = Keyword.get(opts, :text_search, "")

case author do
# Admin authors have access to every project
%{system_role_id: ^admin_role_id} when admin_show_all ->
browse_projects_as_admin(paging, sorting, include_deleted, text_search)

_ ->
browse_projects_as_author(author, paging, sorting, include_deleted, text_search)
end
if Accounts.has_admin_role?(author) and admin_show_all,
do: browse_projects_as_admin(paging, sorting, include_deleted, text_search),
else: browse_projects_as_author(author, paging, sorting, include_deleted, text_search)
end

defp browse_projects_as_admin(
Expand Down
2 changes: 1 addition & 1 deletion lib/oli/delivery/audience.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule Oli.Delivery.Audience do
end

def audience_role(%Author{} = author, _section_slug) do
if Accounts.is_admin?(author) do
if Accounts.is_system_admin?(author) do
:instructor
else
:student
Expand Down
2 changes: 1 addition & 1 deletion lib/oli/utils/seeder/project.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ defmodule Oli.Utils.Seeder.Project do
Author.noauth_changeset(%Author{}, %{
email: "#{Slug.slugify(name)}@test.com",
given_name: name,
system_role_id: SystemRole.role_id().admin
system_role_id: SystemRole.role_id().system_admin
})
|> Repo.insert()

Expand Down
2 changes: 1 addition & 1 deletion lib/oli_web/components/delivery/actions/actions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ defmodule OliWeb.Components.Delivery.Actions do
user_role_data: @user_role_data,
has_payment: has_payment,
current_user: current_user,
is_admin: Accounts.is_admin?(current_user),
is_admin: Accounts.has_admin_role?(current_user),
is_suspended?: is_suspended?
)}
end
Expand Down
10 changes: 7 additions & 3 deletions lib/oli_web/components/delivery/user_account_menu.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ defmodule OliWeb.Components.Delivery.UserAccountMenu do
alias OliWeb.Common.SessionContext
alias OliWeb.Common.React

@system_admin_role_ids [
SystemRole.role_id().system_admin,
SystemRole.role_id().account_admin,
SystemRole.role_id().content_admin
]

attr(:ctx, SessionContext)
attr(:section, Section)
attr(:is_liveview, :boolean, default: false)
Expand Down Expand Up @@ -131,10 +137,8 @@ defmodule OliWeb.Components.Delivery.UserAccountMenu do
end

defp signout_path(%SessionContext{user: user_or_admin}) do
admin_role_id = SystemRole.role_id().admin

case user_or_admin do
%Author{system_role_id: ^admin_role_id} ->
%Author{system_role_id: system_role_id} when system_role_id in @system_admin_role_ids ->
Routes.authoring_session_path(OliWeb.Endpoint, :signout, type: :author)

_ ->
Expand Down
8 changes: 6 additions & 2 deletions lib/oli_web/components/delivery/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,15 @@ defmodule OliWeb.Components.Delivery.Utils do
ContextRoles.get_role(:context_learner)
]

@system_admin_role_id SystemRole.role_id().admin
@system_admin_role_ids [
SystemRole.role_id().system_admin,
SystemRole.role_id().account_admin,
SystemRole.role_id().content_admin
]

def user_role(section, user) do
case user do
%Author{system_role_id: @system_admin_role_id} ->
%Author{system_role_id: system_role_id} when system_role_id in @system_admin_role_ids ->
:system_admin

%Author{} ->
Expand Down
4 changes: 2 additions & 2 deletions lib/oli_web/controllers/activity_bank_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule OliWeb.ActivityBankController do
"project_id" => project_slug
}) do
author = conn.assigns[:current_author]
is_admin? = Accounts.is_admin?(author)
is_admin? = Accounts.at_least_content_admin?(author)

case Oli.Authoring.Editing.BankEditor.create_context(project_slug, author) do
{:ok, context} ->
Expand All @@ -42,7 +42,7 @@ defmodule OliWeb.ActivityBankController do
}) do
user = conn.assigns.current_user
author = conn.assigns.current_author
is_admin? = Accounts.is_admin?(author)
is_admin? = Accounts.at_least_content_admin?(author)

offset =
Map.get(conn.query_params, "offset", "0")
Expand Down
14 changes: 8 additions & 6 deletions lib/oli_web/controllers/api/activity_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule OliWeb.Api.ActivityController do
use OliWeb, :controller
use OpenApiSpex.Controller

alias Oli.Accounts
alias Oli.Authoring.Editing.ActivityEditor
alias Oli.Delivery.Attempts.ActivityLifecycle.Evaluate, as: ActivityEvaluation
alias Oli.Delivery.Attempts.ActivityLifecycle
Expand Down Expand Up @@ -430,12 +431,13 @@ defmodule OliWeb.Api.ActivityController do
defp is_preview_mode?(_), do: false

defp has_access?(conn, user, section_slug, is_preview_mode) do
if is_preview_mode do
is_admin? = Oli.Accounts.is_admin?(conn.assigns[:current_author])
Sections.is_instructor?(user, section_slug) or is_admin?
else
Sections.is_enrolled?(user.id, section_slug)
end
current_author = conn.assigns[:current_author]

if is_preview_mode,
do:
Sections.is_instructor?(user, section_slug) or
Accounts.at_least_content_admin?(current_author),
else: Sections.is_enrolled?(user.id, section_slug)
end

# --------- END DELIVERY PREVIEW ---------
Expand Down
3 changes: 2 additions & 1 deletion lib/oli_web/controllers/api/scheduling_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule OliWeb.Api.SchedulingController do
alias Oli.Delivery.Sections.Scheduling
alias Oli.Delivery.Sections
alias Oli.Delivery.Sections.SectionResource
alias Oli.Accounts

import OliWeb.Api.Helpers

Expand Down Expand Up @@ -191,7 +192,7 @@ defmodule OliWeb.Api.SchedulingController do
# (authoring) admins
defp can_access_section?(conn, section) do
Sections.is_instructor?(conn.assigns.current_user, section.slug) or
Oli.Accounts.is_admin?(conn.assigns.current_author) or
Accounts.at_least_content_admin?(conn.assigns.current_author) or
Sections.is_admin?(conn.assigns.current_user, section.slug)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/oli_web/controllers/bibliography_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule OliWeb.BibliographyController do
"project_id" => project_slug
}) do
author = conn.assigns[:current_author]
is_admin? = Accounts.is_admin?(author)
is_admin? = Accounts.has_admin_role?(author)

case Oli.Authoring.Editing.BibliographyEditor.create_context(project_slug, author) do
{:ok, context} ->
Expand Down
Loading

0 comments on commit 1a67d33

Please sign in to comment.