Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
dbc148b
updated travis ci badges & iter0 doc
junyu-w Oct 8, 2016
79445f8
updated code climate badge & iter0 doc
junyu-w Oct 8, 2016
089e1eb
added pivotal tracker project url
junyu-w Oct 8, 2016
703b0be
iter 1: cucumber
Shuotong Oct 14, 2016
cc649b2
Merge remote-tracking branch 'upstream/develop' into develop
junyu-w Oct 15, 2016
ac82d6f
added status of both main repo and forked repo to readme
junyu-w Oct 15, 2016
982f2a3
Merge branch 'cucumber' into develop
junyu-w Oct 15, 2016
a3ada8b
added heroku staging url
junyu-w Oct 15, 2016
acad466
added github login functionality
junyu-w Oct 20, 2016
e594d45
removed copied sample code
junyu-w Oct 20, 2016
65dee30
added bootstrap gems
junyu-w Oct 20, 2016
aeb22e1
added annotate gem and better error gem
junyu-w Oct 20, 2016
a59f53d
added role attr to users
junyu-w Oct 20, 2016
210605a
added migration to create root user
junyu-w Oct 20, 2016
7abeb4d
changed omniauth github path
junyu-w Oct 20, 2016
d78f7b1
added updated application.yml.asc file
junyu-w Oct 20, 2016
b94d7fd
added credential for test env
junyu-w Oct 20, 2016
d71c2da
added whitelist authorized user checking during login
junyu-w Oct 20, 2016
5c127d4
fixed root user migration & added success msg for github signing in
junyu-w Oct 20, 2016
fa7c364
updated env file
junyu-w Oct 20, 2016
3d0caf7
passed tests assumed no whitelist functionality
junyu-w Oct 20, 2016
bab3832
removed test from env file
junyu-w Oct 20, 2016
a70594d
fixed bug in user login feature
junyu-w Oct 20, 2016
1a1b511
removed duplicated notice msg
junyu-w Oct 20, 2016
f99a57d
updated env file to include staging environment
junyu-w Oct 21, 2016
d9eba9a
removed whitelist checking
junyu-w Oct 21, 2016
8e3e1bd
added user uid validation
junyu-w Oct 21, 2016
327480a
removed uid validation because of conflict with admin
junyu-w Oct 21, 2016
f9959ab
front-end added
junyu-w Oct 21, 2016
76378af
added user authentication
junyu-w Oct 21, 2016
c47f8dd
updated schema & staging env encryption key
junyu-w Oct 21, 2016
d816160
Add whitelist feature
ysiad Oct 21, 2016
3a2ce5e
fixed old tests except for view metric sample with secrets & removed …
junyu-w Oct 21, 2016
10790f9
removed redundant user session check
junyu-w Oct 21, 2016
a1f8e70
add createe user in whitelist
ysiad Oct 21, 2016
de004bd
add feature: delete accounts from whitelist
ysiad Oct 21, 2016
65ce3df
add feature: delete accounts from whitelist
ysiad Oct 21, 2016
51f6f83
Merge conflicts
ysiad Oct 21, 2016
263c8ac
Fix bugs for whitelist
ysiad Oct 22, 2016
b36fe48
Merge branch 'feature/login-with-github' of https://github.com/DrakeW…
ysiad Oct 22, 2016
0153f32
Fix bugs
ysiad Oct 22, 2016
45bb9f5
Fix migration issues
ysiad Oct 22, 2016
3615568
Fix features as the pull request comments suggested
ysiad Oct 24, 2016
b81d931
Fix issues in previous pull request
ysiad Oct 24, 2016
ec69272
Fix issues in migration file
ysiad Oct 24, 2016
4293e63
Deleted unused files
ysiad Oct 24, 2016
d1c233e
Fix issues
ysiad Oct 24, 2016
8285af4
pull
ysiad Oct 31, 2016
0907a2f
adjust whitelist
ysiad Oct 31, 2016
04b2603
Merge branch 'develop' of https://github.com/DrakeW/projectscope into…
ysiad Nov 4, 2016
077a0cf
functionality complete
ysiad Nov 4, 2016
2e5118b
Change the whitelist to contain github username
ysiad Nov 4, 2016
c0fefcc
modify the description for feature test
ysiad Nov 4, 2016
47a7c62
Fix issues as the comments suggested
ysiad Nov 4, 2016
effd2c2
Add session destroy for whitelist
ysiad Nov 4, 2016
2e6a5da
Fix whitelist delete identity issues
ysiad Nov 5, 2016
753c59f
Fix conflict with develop branch
ysiad Nov 5, 2016
14a16f1
Fix issues in readme
ysiad Nov 5, 2016
87d53e7
Add the line back in Readme
ysiad Nov 5, 2016
ce6ad7b
better formatting
ysiad Nov 20, 2016
b6315dd
Merge branch 'feature/whitelist' of https://github.com/DrakeW/project…
ysiad Nov 20, 2016
fbc4f30
change the order of the link
ysiad Nov 20, 2016
82ac558
Modified column name
ysiad Nov 20, 2016
fddb6a1
set attr_accessible
ysiad Nov 20, 2016
644302a
fix accessor
ysiad Nov 20, 2016
4849a2b
pull from dev branch
ysiad Dec 5, 2016
7fb3aae
1. set role default to student; 2.change whitelist to privilege contr…
ysiad Dec 5, 2016
5a81b40
change the prompt sentence
ysiad Dec 5, 2016
8410cbe
modified the prompt message
ysiad Dec 5, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class ProjectsController < ApplicationController
before_action :set_project, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!


# GET /projects
# GET /projects.json

Expand Down
87 changes: 87 additions & 0 deletions app/controllers/whitelists_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
class WhitelistsController < ApplicationController
before_action :check_if_coach

# GET /whitelists
def index
@permitted_users = User.all
end

# GET /whitelists/new
def new
@authorized_user = Whitelist.new(user_params)
end

# POST /whitelists/
def create
username = params[:username]
if Whitelist.has_username?(username)
flash[:notice] = "User #{username} already exists in whitelist. "
else
begin
Whitelist.create!(username: username)
flash[:notice] = "Add user #{username} successfully."
rescue ActiveRecord::RecordInvalid
flash[:notice] = "Invalid username format."
end
end
redirect_to whitelists_path
end

# DELETE /whitelists/
def destroy
user = Whitelist.find(params[:id])
if user.username.eql?(current_user.provider_username)
flash[:notice] = "Delete yourself from the whitelist is not allowed. "
else
user.destroy!
flash[:notice] = "User is deleted successfully."
end
redirect_to whitelists_path
end

def check_if_coach
unless current_user.role.eql?("admin") or current_user.role.eql?("coach")
flash[:notice] = "You have no privilege to manipulate privilege control."
redirect_to projects_url
end
end

def upgrade
unless current_user.role.eql?("admin") or current_user.role.eql?("coach")
flash[:alert] = "You do not have privilege to change other user's role. "
redirect_to whitelists_path
return
end
user = User.find(params[:id])
if user.role.eql?("admin")
flash[:alert] = "Admin role cannot be changed."
end
if user.role.eql?("student")
user.change_role("coach")
end
redirect_to whitelists_path
end

def downgrade
unless current_user.role.eql?("admin") or current_user.role.eql?("coach")
flash[:alert] = "You do not have privilege to change other user's role. "
redirect_to whitelists_path
return
end
user = User.find(params[:id])
if user.role.eql?("admin")
flash[:alert] = "Admin role cannot be changed."
end
if user.role.eql?("coach")
user.change_role("student")
end
redirect_to whitelists_path
end

private

def user_params
params.require(:whitelist).permit(:username)
end

end
6 changes: 6 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class User < ActiveRecord::Base

ADMIN = "admin"
COACH = "coach"
STUDENT = "student"

def self.from_omniauth(auth)
email = auth.info.email.nil? ? auth.extra.raw_info.email : auth.info.email
Expand All @@ -59,6 +60,11 @@ def is_admin?
self.role == ADMIN
end

def change_role(role)
self.role = role
self.save!
end

def preferred_projects
self.selected_projects = Project.all if self.selected_projects.empty?
self.selected_projects
Expand Down
15 changes: 15 additions & 0 deletions app/models/whitelist.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# == Schema Information
#
# Table name: whitelists
#
# id :integer not null, primary key
# username :string
#

class Whitelist < ActiveRecord::Base
validates_format_of :username,:with => /\A[a-z0-9\-_]+\z/i

def self.has_username?(username)
return !Whitelist.find_by_username(username).nil?
end
end
1 change: 1 addition & 0 deletions app/views/devise/sessions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<h2><strong>Welcome to ProjectScope</strong></h2>
</div>
<div class="panel-body">

<%= render "devise/shared/links" %>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions app/views/devise/shared/_links.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<%= link_to "Sign up", new_registration_path(resource_name) %><br />
<% end -%>


<%- if devise_mapping.confirmable? && controller_name != 'confirmations' %>
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br />
<% end -%>
Expand Down
15 changes: 13 additions & 2 deletions app/views/projects/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@
= '%.2f'.try(:%,sample.try(:score) || 0.0)
#{raw sample.try(:image)}

%br/
-unless current_user.role.eql?("coach") or current_user.role.eql?("admin")
%br/

= link_to 'Create New Project', new_project_path, :class => "btn btn-primary"

-else
%br/

= link_to 'Privilege Control', whitelists_path, :class => "btn btn-primary"

%br/

= link_to 'Create New Project', new_project_path, :class => "btn btn-primary"

= link_to 'Create New Project', new_project_path, :class => "btn btn-primary"
5 changes: 5 additions & 0 deletions app/views/whitelists/_add.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
= form_tag whitelists_path do
= field_set_tag do
= label_tag :username, 'GitHub username'
= text_field_tag :username
= submit_tag 'Add'
50 changes: 50 additions & 0 deletions app/views/whitelists/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
:css
#Users{
border-collapse: collapse;
width: 80%;
margin-right: auto;
}

#Users td, #Users th {
border: 1px solid #ddd;
padding: 8px;
}

#Users tr:nth-child(even){background-color: #f2f2f2;}

#Users tr:hover {background-color: #ddd;}

#Users th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #337ab7;
color: white;
}
#Users tr {
text-align: left;
}

%h1 Privilege Control

%table#Users
%thead
%tr
%th GitHub Account
%th Role
%th Action

%tbody
- @permitted_users.each do |user|
%tr
%td= user.provider_username
%td= user.role
-if user.role.eql?("coach")
%td= link_to "Downgrade to student", downgrade_user_path(user), :method => :get, data: { confirm: "Do you want to downgrade user #{user.provider_username} from the whitelist?" }, :user => user
-elsif user.role.eql?("student")
%td= link_to "Upgrade to coach", upgrade_user_path(user), :method => :get, data: { confirm: "Do you want to upgrade user #{user.provider_username} from the whitelist?" }, :user => user
-else
%td

%br/
= link_to 'Back to project page', projects_url
6 changes: 6 additions & 0 deletions app/views/whitelists/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Authorize Users to have access to ProjectScope</h1>

<%= render 'add' %>

<input name="authenticity_token" value="<%= form_authenticity_token %>" type="hidden">
<%= link_to 'Back', whitelists_path %>
7 changes: 6 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
Rails.application.routes.draw do
resources :users, :only => [:show, :update], :path => "u"
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }, :skip => [:password]

resources :projects
root 'projects#index'
resources :whitelists

get '/whitelists/upgrade/:id', :to => 'whitelists#upgrade', :as => 'upgrade_user'
get '/whitelists/downgrade/:id', :to => 'whitelists#downgrade', :as => 'downgrade_user'

end
2 changes: 1 addition & 1 deletion db/migrate/20161020034239_add_role_to_users.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class AddRoleToUsers < ActiveRecord::Migration
def change
add_column :users, :role, :string, null: false, default: "coach"
add_column :users, :role, :string, null: false, default: "student"
end
end
8 changes: 7 additions & 1 deletion db/migrate/20161020040440_create_root_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ def up
password: Devise.friendly_token[0,20],
provider_username: "DrakeW",
provider: "github",
role: "admin")
role: "coach")
User.create!(email: "fox@cs.berkeley.edu",
password: Devise.friendly_token[0,20],
provider_username: "armandofox",
provider: "github",
role: "admin")
User.create!(email: "jiachengwu@berkeley.edu",
password: Devise.friendly_token[0,20],
provider_username: "ysiad",
provider: "github",
role: "coach")
end

def down
User.where(email: "junyuw@berkeley.edu", provider: "github").first.destroy
User.where(email: "fox@cs.berkeley.edu", provider: "github").first.destroy
User.where(email: "jiachengwu@berkeley.edu", provider: "github").first.destroy
end
end
9 changes: 9 additions & 0 deletions db/migrate/20161022053537_create_whitelists.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateWhitelists < ActiveRecord::Migration
def change
create_table :whitelists, :force => true do |t|
t.string :username # default: "", null: false
end
add_index :whitelists, :username
end
end

11 changes: 11 additions & 0 deletions db/migrate/20161022053828_create_authorized_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateAuthorizedUser < ActiveRecord::Migration

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we generally try and avoid manipulating data in our migrations - maybe put this in seed file?

https://robots.thoughtbot.com/data-migrations-in-rails

def up
Whitelist.create!(username: "DrakeW")
Whitelist.create!(username: "armandofox")
end

def down
Whitelist.where(username: "DrakeW").first.destroy
Whitelist.where(username: "armandofox").first.destroy
end
end
6 changes: 6 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,10 @@
add_index "users", ["provider_username"], name: "index_users_on_provider_username", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

create_table "whitelists", force: :cascade do |t|
t.string "username"
end

add_index "whitelists", ["username"], name: "index_whitelists_on_username"

end
2 changes: 2 additions & 0 deletions features/github_user_login.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ Feature: GitHub User Login

Scenario: github user login with email
Given I am on the login page
And "test-coach" is in the whitelist
And I have a valid github account with email "test-coach@test.com" username "test-coach"
When I follow "Sign in with GitHub"
Then I should be on the home page
And I should see "Signed in successfully."

Scenario: github user login without email
Given I am on the login page
And "test-coach" is in the whitelist
And I have a valid github account with email "" username "test-coach"
When I follow "Sign in with GitHub"
Then I should be on the home page
Expand Down
1 change: 1 addition & 0 deletions features/step_definitions/project_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
end

And(/^I am logged in$/) do
Whitelist.create :username => "test-coach"
visit path_to("the login page")
OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new(
{
Expand Down
16 changes: 16 additions & 0 deletions features/step_definitions/whitelist_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Given(/^"([^"]*)" is in the whitelist$/) do |username|
Whitelist.create!(username: username)
end

Then /^I should be admin$/ do
expect(current_user.role).to eq "admin"
end

Given /^I enter the whitelist page$/ do
visit path_to("the whitelist page")
sleep(1)
end

When /^I follow the first "Delete"$/ do
first(:link, "Delete").click
end
4 changes: 4 additions & 0 deletions features/support/paths.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def path_to(page_name)
"/projects/#{Project.find_by(name: $1).id}/edit"
when /^the login page/ then
'/users/sign_in'
when /^the whitelist page/ then
'/whitelists'
when /^the whitelist management page/ then
'/whitelists/new'
# Add more mappings here.
# Here is an example that pulls values out of the Regexp:
#
Expand Down
Loading