From 3aed54933ed28d75bbd26b4cc514afca6eb767e7 Mon Sep 17 00:00:00 2001 From: Michelle Fan Date: Sun, 16 Dec 2018 20:24:19 -0800 Subject: [PATCH] Add Permissions Framework (#43) * created user role column * authentication login test * Added in initial changes for authentication * Small changes to spelling and role declaration * Reverse permissions * Added reverse changes to other sections for readability * changed unless to if for the scope in users * deleted admin_user.rb files * Testing * Testing * Testing changes * Changing permissions for users * Edits * deleted admin_user.rb from models * Modified the New User page so admins can set roles when creating new users * Prevented users from being created with null role value * update schema file * removed extra roles column * rubocop style fixes * boolean changes in user.rb * revert users.rb change * now blocks access to team_switch requests, redirecting to back page * comments on teamswitch requests * Add dropdown to select user role when creating new user (#38) * Modified the New User page so admins can set roles when creating new users * Prevented users from being created with null role value * update schema file * removed extra roles column * rubocop style fixes * rubocop autocorrect * added roles to dancer_test * added id to dancers to force unique constraint * added usernames to users.yml to fix unique constraint * all dancers tests pass * Non-admin users can no longer access team switch request page and users page (#41) * style fixes * redirects to dashboard page when user tries to access team switch request * block viewing users for directors, can't change own password if director * Only Show Team Switch Requests/Users Buttons for Admin (#40) * changed unless to if * test - switched true/false for can view * migrations * only add to menu if user is admin * used guard clause and fixed circleci * hide user button for nonadmin * style fix avoiding guard clause * fixed unexpected end * indentation style fix * extra line rubocop fix * add question mark to users * slight syntax --- .vscode/settings.json | 50 ++++---- app/admin/team_switch_requests.rb | 8 +- app/admin/teams.rb | 1 + app/admin/users.rb | 12 ++ app/models/team_switch_request.rb | 4 + app/models/user.rb | 25 +++- cucumber-java/.idea/compiler.xml | 13 +++ cucumber-java/.idea/encodings.xml | 6 + cucumber-java/.idea/misc.xml | 14 +++ cucumber-java/.idea/vcs.xml | 6 + cucumber-java/.idea/workspace.xml | 109 ++++++++++++++++++ cucumber-java/pom.xml | 65 +++++++++++ cucumber-java/src/main/java/test/App.java | 13 +++ cucumber-java/src/test/java/test/AppTest.java | 20 ++++ cucumber-java/target/classes/test/App.class | Bin 0 -> 523 bytes .../compile/default-compile/createdFiles.lst | 1 + .../compile/default-compile/inputFiles.lst | 1 + .../default-testCompile/createdFiles.lst | 1 + .../default-testCompile/inputFiles.lst | 1 + .../target/test-classes/test/AppTest.class | Bin 0 -> 455 bytes .../20181020191320_add_role_to_users.rb | 5 + .../20181103192530_change_user_role_null.rb | 5 + .../20181103192805_create_director_users.rb | 6 + db/schema.rb | 6 +- db/seeds.rb | 8 +- features/step_definitions/admin_steps.rb | 26 ----- test/fixtures/dancers.yml | 46 ++++++-- test/fixtures/teams.yml | 4 - test/fixtures/users.yml | 10 +- test/models/dancer_test.rb | 56 ++++++++- 30 files changed, 447 insertions(+), 75 deletions(-) create mode 100644 cucumber-java/.idea/compiler.xml create mode 100644 cucumber-java/.idea/encodings.xml create mode 100644 cucumber-java/.idea/misc.xml create mode 100644 cucumber-java/.idea/vcs.xml create mode 100644 cucumber-java/.idea/workspace.xml create mode 100644 cucumber-java/pom.xml create mode 100644 cucumber-java/src/main/java/test/App.java create mode 100644 cucumber-java/src/test/java/test/AppTest.java create mode 100644 cucumber-java/target/classes/test/App.class create mode 100644 cucumber-java/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst create mode 100644 cucumber-java/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst create mode 100644 cucumber-java/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst create mode 100644 cucumber-java/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst create mode 100644 cucumber-java/target/test-classes/test/AppTest.class create mode 100644 db/migrate/20181020191320_add_role_to_users.rb create mode 100644 db/migrate/20181103192530_change_user_role_null.rb create mode 100644 db/migrate/20181103192805_create_director_users.rb delete mode 100644 features/step_definitions/admin_steps.rb diff --git a/.vscode/settings.json b/.vscode/settings.json index f99518d9..915c1d4b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,25 +1,25 @@ -{ - "[html.erb]": { - "editor.detectIndentation": false, - "editor.insertSpaces": true, - "editor.tabSize": 2, - }, - "[ruby]": { - "editor.detectIndentation": false, - "editor.insertSpaces": true, - "editor.tabSize": 2, - }, - - "editor.insertSpaces": true, - "editor.rulers": [100], - "editor.tabSize": 2, - "editor.wordWrap": "bounded", - "editor.wordWrapColumn": 100, - "files.insertFinalNewline": true, - "files.trimFinalNewlines": true, - "files.trimTrailingWhitespace": true, - "workbench.editor.closeOnFileDelete": false, - - // https://code.visualstudio.com/docs/editor/integrated-terminal - "terminal.integrated.shell.windows": "C:\\Windows\\System32\\bash.exe", -} +{ + "[html.erb]": { + "editor.detectIndentation": false, + "editor.insertSpaces": true, + "editor.tabSize": 2, + }, + "[ruby]": { + "editor.detectIndentation": false, + "editor.insertSpaces": true, + "editor.tabSize": 2, + }, + + "editor.insertSpaces": true, + "editor.rulers": [100], + "editor.tabSize": 2, + "editor.wordWrap": "bounded", + "editor.wordWrapColumn": 100, + "files.insertFinalNewline": true, + "files.trimFinalNewlines": true, + "files.trimTrailingWhitespace": true, + "workbench.editor.closeOnFileDelete": false, + + // https://code.visualstudio.com/docs/editor/integrated-terminal + "terminal.integrated.shell.windows": "C:\\Windows\\System32\\bash.exe", +} diff --git a/app/admin/team_switch_requests.rb b/app/admin/team_switch_requests.rb index 02620c61..a26b308f 100644 --- a/app/admin/team_switch_requests.rb +++ b/app/admin/team_switch_requests.rb @@ -12,6 +12,8 @@ # permitted # end + before_action :role_check + permit_params do [ :name, @@ -57,6 +59,11 @@ end controller do + # checks if user can view the team switch requests page + def role_check + redirect_to "/admin", alert: "You can't view the team switch requests page!!! >:( uwu" unless current_user.can_view_team_switch? + end + def action_methods if current_user.can_modify_all_teams? super @@ -147,7 +154,6 @@ def process_team_switch_request_into_team(team_switch_request_id, team_id) selectable_column # https://github.com/activeadmin/activeadmin/issues/1995#issuecomment-15846811 TeamSwitchRequest.content_columns.each { |col| column col.name.to_sym } - column :old_team column :new_team column :current_team do |team_switch_request| diff --git a/app/admin/teams.rb b/app/admin/teams.rb index 2e558092..c616629e 100644 --- a/app/admin/teams.rb +++ b/app/admin/teams.rb @@ -11,6 +11,7 @@ # permitted << :other if params[:action] == 'create' && current_user.admin? # permitted # end + scope_to :current_user permit_params do [ diff --git a/app/admin/users.rb b/app/admin/users.rb index d5b5a7af..c8fe67bd 100644 --- a/app/admin/users.rb +++ b/app/admin/users.rb @@ -1,11 +1,20 @@ ActiveAdmin.register User do + before_action :role_check permit_params( :username, :password, :password_confirmation, + :role, team_ids: [], # Necessary in order to properly link users and teams ) + controller do + # checks if user can view the users page + def role_check + redirect_to "/admin", alert: "You can't view the users page!!! >:( uwu" unless current_user.can_view_users? + end + end + index do selectable_column id_column @@ -13,6 +22,7 @@ column :current_sign_in_at column :sign_in_count column :created_at + column :role column "Teams" do |user| user.teams.map do |team| link_to team.name, admin_team_path(team) @@ -25,6 +35,7 @@ filter :current_sign_in_at filter :sign_in_count filter :created_at + filter :role form do |f| f.inputs do @@ -33,6 +44,7 @@ f.input :password_confirmation # Creates the selection menu so the user can choose a team f.input :teams, collection: Team.all.map { |team| [team.name, team.id] } + f.input :role, as: :select, collection: User.roles.keys, include_blank: false, allow_blank: false end f.actions end diff --git a/app/models/team_switch_request.rb b/app/models/team_switch_request.rb index 65f41bd6..1a3a695f 100644 --- a/app/models/team_switch_request.rb +++ b/app/models/team_switch_request.rb @@ -1,5 +1,9 @@ class TeamSwitchRequest < ApplicationRecord # https://blog.bigbinary.com/2016/02/15/rails-5-makes-belong-to-association-required-by-default.html + ActiveAdmin.register TeamSwitchRequest do + menu if: proc { current_user.admin? } + end + belongs_to :dancer, optional: true has_and_belongs_to_many :available_teams, class_name: "Team", join_table: :team_switch_requests_available_teams belongs_to :old_team, class_name: "Team", optional: true diff --git a/app/models/user.rb b/app/models/user.rb index 61588e02..6344aba1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,5 +1,10 @@ class User < ApplicationRecord + ActiveAdmin.register User do + menu if: proc { current_user.admin? } + end + has_and_belongs_to_many :teams + enum role: [:director, :admin] # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise( @@ -33,11 +38,11 @@ def board_privileges? end def can_modify_users? - true + board_privileges? end def can_create_dancer? - true + board_privileges? end def can_modify_next_dancer_id? @@ -71,4 +76,20 @@ def accessible_dancer_fields def can_do_randomization? true end + + def can_view_team_switch? + if admin? + true + else + false + end + end + + def can_view_users? + if admin? + true + else + false + end + end end diff --git a/cucumber-java/.idea/compiler.xml b/cucumber-java/.idea/compiler.xml new file mode 100644 index 00000000..8fd891eb --- /dev/null +++ b/cucumber-java/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/cucumber-java/.idea/encodings.xml b/cucumber-java/.idea/encodings.xml new file mode 100644 index 00000000..b26911bd --- /dev/null +++ b/cucumber-java/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/cucumber-java/.idea/misc.xml b/cucumber-java/.idea/misc.xml new file mode 100644 index 00000000..bcc0317e --- /dev/null +++ b/cucumber-java/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/cucumber-java/.idea/vcs.xml b/cucumber-java/.idea/vcs.xml new file mode 100644 index 00000000..6c0b8635 --- /dev/null +++ b/cucumber-java/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/cucumber-java/.idea/workspace.xml b/cucumber-java/.idea/workspace.xml new file mode 100644 index 00000000..96ee94a2 --- /dev/null +++ b/cucumber-java/.idea/workspace.xml @@ -0,0 +1,109 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1540192006786 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/cucumber-java/pom.xml b/cucumber-java/pom.xml new file mode 100644 index 00000000..f0c265c1 --- /dev/null +++ b/cucumber-java/pom.xml @@ -0,0 +1,65 @@ + + + + 4.0.0 + + io.cucumber + cucumber-java + 4.0.0 + + cucumber-java + + http://www.example.com + + + UTF-8 + 1.7 + 1.7 + + + + + junit + junit + 4.11 + test + + + + + + + + maven-clean-plugin + 3.0.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.7.0 + + + maven-surefire-plugin + 2.20.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + + diff --git a/cucumber-java/src/main/java/test/App.java b/cucumber-java/src/main/java/test/App.java new file mode 100644 index 00000000..b8e9695d --- /dev/null +++ b/cucumber-java/src/main/java/test/App.java @@ -0,0 +1,13 @@ +package test; + +/** + * Hello world! + * + */ +public class App +{ + public static void main( String[] args ) + { + System.out.println( "Hello World!" ); + } +} diff --git a/cucumber-java/src/test/java/test/AppTest.java b/cucumber-java/src/test/java/test/AppTest.java new file mode 100644 index 00000000..fc84e0f6 --- /dev/null +++ b/cucumber-java/src/test/java/test/AppTest.java @@ -0,0 +1,20 @@ +package test; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +/** + * Unit test for simple App. + */ +public class AppTest +{ + /** + * Rigorous Test :-) + */ + @Test + public void shouldAnswerWithTrue() + { + assertTrue( true ); + } +} diff --git a/cucumber-java/target/classes/test/App.class b/cucumber-java/target/classes/test/App.class new file mode 100644 index 0000000000000000000000000000000000000000..510514d35a0120b05d9357ebc0692c57ce227801 GIT binary patch literal 523 zcmZuuO;5r=5Pi$nwzUd^;J1P&^k5GjJYYJ17lxCf8yF=MCb5Q$bY-C6q>j@>Q(=nCy?r26slS&cN9+gyM9%kU9 zgcQ(%)#jr8&_JHvIyo5-9Tr)eB9A;HpFjEldri|0*+L3(EYaPa638!8WMc(ILXzUs hRE)4UWXjohd*vi<(~`~omUFNBZ75N&hkFjzNYP5 Dancer - klass = Object.const_get(type.singularize.camelcase) - table.hashes.each do |element| - klass.create!(element) - end -end - -Given(/^I am logged into the admin panel$/) do - visit "/admin/login" - fill_in "Email", with: "admin@example.com" - fill_in "Password", with: "password" - click_button "Login" - if page.respond_to? :should - page.should have_content("Signed in successfully.") - else - assert page.has_content?("Signed in successfully.") - end -end - -Given(/I log in as "(.*?)" with password "(.*?)"$/) do |username, pass| - visit "/admin/login" - fill_in "user[username]", with: username - fill_in "user[password]", with: pass - click_button "Login" -end diff --git a/test/fixtures/dancers.yml b/test/fixtures/dancers.yml index 605dff56..64344708 100644 --- a/test/fixtures/dancers.yml +++ b/test/fixtures/dancers.yml @@ -1,12 +1,38 @@ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html -one: - name: MyString - email: MyString - phone: MyString - reason: MyString - approved_at: MyString - status: MyString - gender: MyString - year: MyString - experience: MyString +howard: + id: 1 + name: Howard Wang + email: howard.wang@berkeley.edu + phone: 999-999-9999 + gender: male + year: sophomore + dance_experience: some + +young: + id: 2 + name: Young Cai + email: youngcai@berkeley.edu + phone: 510-388-9830 + gender: male + year: junior + dance_experience: some + + +patricia: + id: 3 + name: Patricia Yu + email: patriciayu7@berkeley.edu + phone: 510-505-4813 + gender: female + year: sophomore + dance_experience: some + +jeffrey: + id: 4 + name: Jeffrey Chen + email: jeffreydchen@berkeley.edu + phone: 732-640-3705 + gender: male + year: junior + dance_experience: two diff --git a/test/fixtures/teams.yml b/test/fixtures/teams.yml index b2aa8e6b..95b7e344 100644 --- a/test/fixtures/teams.yml +++ b/test/fixtures/teams.yml @@ -1,15 +1,11 @@ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: - type: name: MyString practice_time: MyString locked: MyString - project: MyBoolean two: - type: name: MyString practice_time: MyString locked: MyString - project: MyBoolean diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 80aed36e..db90d2f9 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -4,8 +4,14 @@ # model remove the '{}' from the fixture names and add the columns immediately # below each fixture, per the syntax in the comments below # -one: {} +one: + id: 1 + role: director + username: user1 # column: value # -two: {} +two: + id: 2 + role: admin + username: user2 # column: value diff --git a/test/models/dancer_test.rb b/test/models/dancer_test.rb index aa7a515e..266c3bb3 100644 --- a/test/models/dancer_test.rb +++ b/test/models/dancer_test.rb @@ -1,7 +1,57 @@ require "test_helper" class DancerTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + test "dancers added" do + assert_equal 4, Dancer.count + end + + test "Howard's phone number" do + assert_equal "999-999-9999", dancers(:howard).phone + end + + test "Howard's year" do + assert_equal "sophomore", dancers(:howard).year + end + + test "Howard's dance exp" do + assert_equal "some", dancers(:howard).dance_experience + end + + test "are there 4 dancers added" do + assert_equal 4, Dancer.count + end + + test "young's gender" do + assert_equal "male", dancers(:young).gender + end + + test "young's email" do + assert_equal "youngcai@berkeley.edu", dancers(:young).email + end + + test "Jeffrey's experience" do + assert_equal "two", dancers(:jeffrey).dance_experience + end + + test "Jeffrey's email" do + assert_equal "jeffreydchen@berkeley.edu", dancers(:jeffrey).email + end + + test "Jeffrey's phone" do + assert_equal "732-640-3705", dancers(:jeffrey).phone + end + + test "Patricia's phone number" do + assert_equal "510-505-4813", dancers(:patricia).phone + end + + test "Patricia's email" do + email = "patriciayu7@berkeley.edu" + assert_equal email, dancers(:patricia).email + end + + test "Patricia's experience" do + # should fail + assert_equal "some", dancers(:patricia).dance_experience + end end