diff --git a/app/controllers/account_requests_controller.rb b/app/controllers/account_requests_controller.rb index c4b79e065..79c1e183c 100644 --- a/app/controllers/account_requests_controller.rb +++ b/app/controllers/account_requests_controller.rb @@ -76,7 +76,7 @@ def account_request_params elsif !['Approved', 'Rejected'].include?(params[:account_request][:status]) raise StandardError, 'Status can only be Approved or Rejected' end - params.require(:account_request).permit(:username, :full_name, :email, :status, :introduction, :role_id, :institution_id) + params.require(:account_request).permit(:username, :full_name, :email, :status, :introduction, :role_id, :institution_id, :date_format_pref) end # Create a new user if account request is approved diff --git a/app/controllers/authentication_controller.rb b/app/controllers/authentication_controller.rb index 696c4ab21..5140aa2bb 100644 --- a/app/controllers/authentication_controller.rb +++ b/app/controllers/authentication_controller.rb @@ -9,7 +9,7 @@ def login user = User.find_by(name: params[:user_name]) || User.find_by(email: params[:user_name]) if user&.authenticate(params[:password]) payload = { id: user.id, name: user.name, full_name: user.full_name, role: user.role.name, - institution_id: user.institution.id } + institution_id: user.institution.id, date_format_pref: user.date_format_pref } token = JsonWebToken.encode(payload, 24.hours.from_now) render json: { token: }, status: :ok else diff --git a/app/controllers/institutions_controller.rb b/app/controllers/institutions_controller.rb index 1eaa1f431..4e8596043 100644 --- a/app/controllers/institutions_controller.rb +++ b/app/controllers/institutions_controller.rb @@ -1,7 +1,7 @@ class InstitutionsController < ApplicationController rescue_from ActiveRecord::RecordNotFound, with: :institution_not_found def action_allowed? - has_role?('Instructor') + has_role?('Instructor') || has_role?('Administrator') end # GET /institutions def index diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 83d7352fd..8e608ca40 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -82,7 +82,7 @@ def role_users def user_params params.require(:user).permit(:id, :name, :role_id, :full_name, :email, :parent_id, :institution_id, :email_on_review, :email_on_submission, :email_on_review_of_review, - :handle, :copy_of_emails, :password, :password_confirmation) + :handle, :copy_of_emails, :password, :password_confirmation, :date_format_pref) end def user_not_found diff --git a/app/models/role.rb b/app/models/role.rb index 3cce77975..1440f04dd 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -5,12 +5,12 @@ class Role < ApplicationRecord belongs_to :parent, class_name: 'Role', optional: true has_many :users, dependent: :nullify - # Role IDs - STUDENT_ID = 1 - TEACHING_ASSISTANT_ID = 2 + # Role IDs (must match db/seeds.rb) + SUPER_ADMINISTRATOR_ID = 1 + ADMINISTRATOR_ID = 2 INSTRUCTOR_ID = 3 - ADMINISTRATOR_ID = 4 - SUPER_ADMINISTRATOR_ID = 5 + TEACHING_ASSISTANT_ID = 4 + STUDENT_ID = 5 def super_administrator? name['Super Administrator'] @@ -67,4 +67,4 @@ def as_json(options = nil) options = options || {} # Ensure options is a hash super(options.merge({ only: %i[id name parent_id] })) end -end +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index 0e77e25dc..4aedcd459 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -21,11 +21,11 @@ class User < ApplicationRecord has_many :teams, through: :teams_users has_many :participants - scope :students, -> { where role_id: Role::STUDENT } - scope :tas, -> { where role_id: Role::TEACHING_ASSISTANT } - scope :instructors, -> { where role_id: Role::INSTRUCTOR } - scope :administrators, -> { where role_id: Role::ADMINISTRATOR } - scope :superadministrators, -> { where role_id: Role::SUPER_ADMINISTRATOR } + scope :students, -> { where role_id: Role::STUDENT_ID } + scope :tas, -> { where role_id: Role::TEACHING_ASSISTANT_ID } + scope :instructors, -> { where role_id: Role::INSTRUCTOR_ID } + scope :administrators, -> { where role_id: Role::ADMINISTRATOR_ID } + scope :superadministrators, -> { where role_id: Role::SUPER_ADMINISTRATOR_ID } delegate :student?, to: :role delegate :ta?, to: :role @@ -34,17 +34,17 @@ class User < ApplicationRecord delegate :super_administrator?, to: :role def self.instantiate(record) - case record.role - when Role::TEACHING_ASSISTANT + case record.role_id + when Role::TEACHING_ASSISTANT_ID record.becomes(Ta) - when Role::INSTRUCTOR + when Role::INSTRUCTOR_ID record.becomes(Instructor) - when Role::ADMINISTRATOR + when Role::ADMINISTRATOR_ID record.becomes(Administrator) - when Role::SUPER_ADMINISTRATOR + when Role::SUPER_ADMINISTRATOR_ID record.becomes(SuperAdministrator) else - super + record end end @@ -72,10 +72,10 @@ def reset_password # Get instructor_id of the user, if the user is TA, # return the id of the instructor else return the id of the user for superior roles def instructor_id - case role - when Role::INSTRUCTOR, Role::ADMINISTRATOR, Role::SUPER_ADMINISTRATOR + case role_id + when Role::INSTRUCTOR_ID, Role::ADMINISTRATOR_ID, Role::SUPER_ADMINISTRATOR_ID id - when Role::TEACHING_ASSISTANT + when Role::TEACHING_ASSISTANT_ID my_instructor else raise NotImplementedError, "Unknown role: #{role.name}" @@ -153,4 +153,4 @@ def generate_jwt JWT.encode({ id: id, exp: 60.days.from_now.to_i }, Rails.application.credentials.secret_key_base) end -end +end \ No newline at end of file diff --git a/db/migrate/20251201034750_add_date_format_pref_to_users.rb b/db/migrate/20251201034750_add_date_format_pref_to_users.rb new file mode 100644 index 000000000..dba4eb313 --- /dev/null +++ b/db/migrate/20251201034750_add_date_format_pref_to_users.rb @@ -0,0 +1,5 @@ +class AddDateFormatPrefToUsers < ActiveRecord::Migration[8.0] + def change + add_column :users, :date_format_pref, :string, default: "mm/dd/yyyy" + end +end diff --git a/db/schema.rb b/db/schema.rb index d3a15fcfa..0a1895b07 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2025_10_29_071649) do +ActiveRecord::Schema[8.0].define(version: 2025_12_01_034750) do create_table "account_requests", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.string "username" t.string "full_name" @@ -411,6 +411,7 @@ t.bigint "institution_id" t.bigint "role_id", null: false t.bigint "parent_id" + t.string "date_format_pref", default: "mm/dd/yyyy" t.index ["institution_id"], name: "index_users_on_institution_id" t.index ["parent_id"], name: "index_users_on_parent_id" t.index ["role_id"], name: "index_users_on_role_id"