Skip to content

Commit

Permalink
Adds a multiuser/singleuser switch and admin users.
Browse files Browse the repository at this point in the history
The first user to sign-up gets the admin bit. It is currently a secret
page "/admin". It is secret only through the fact that there is no link
to it. The multiuser switch will not allow any new accounts.

There is no way to re-assign admin or add new admins at the moment.

Look at that though, that meaningless helper somebody added
'admin_only!' is now actually meaningful. :)
  • Loading branch information
wilkie committed Aug 5, 2014
1 parent d39d237 commit 4151f15
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 31 deletions.
5 changes: 0 additions & 5 deletions app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
class AdminController < ApplicationController
def index
logger.debug "current_user:"
logger.debug current_user
return if admin_only!

@admin = admin_info
end

def update
logger.debug session
logger.debug "current_user:"
logger.debug current_user
return if admin_only!

admin_info.multiuser = (params["multiuser"] == "on")
Expand Down
13 changes: 12 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class ApplicationController < ActionController::Base

helper_method :current_user
helper_method :logged_in?
helper_method :admin_info
helper_method :admin_only!
helper_method :require_user
helper_method :set_params_page
Expand All @@ -29,11 +30,21 @@ def logged_in?
current_user
end

# This function will retrieve the administration settings for this system.
def admin_info
@admin_info ||= (Admin.first || Admin.create)
end

# Our `admin_only!` helper will only let admin users visit the page. If
# they're not an admin, we redirect them to either / or the page that we
# specified when we called it.
def admin_only!(opts = {:return => "/"})
redirect_with_sorry(opts) unless logged_in? && current_user.admin?
unless (logged_in? && current_user.admin?)
redirect_with_sorry(opts)
return true
end

false
end

# Similar to `admin_only!`, `require_login!` only lets logged in users access
Expand Down
44 changes: 24 additions & 20 deletions app/controllers/auth_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,34 @@ def auth
redirect_to edit_user_path(current_user)
return
else
if admin_info.can_create_user?
# This situation here really sucks. I'd like to do something better,
# and maybe the correct answer is just session[:auth] = auth. This
# might be a nice refactoring.
session[:uid] = auth['uid']
session[:provider] = auth['provider']
session[:name] = auth['info']['name']
session[:nickname] = auth['info']['nickname']
session[:website] = auth['info']['urls']['Website']
session[:description] = auth['info']['description']
session[:image] = auth['info']['image']
session[:email] = auth['info']['email']
session[:oauth_token] = auth['credentials']['token']
session[:oauth_secret] = auth['credentials']['secret']

# This situation here really sucks. I'd like to do something better,
# and maybe the correct answer is just session[:auth] = auth. This
# might be a nice refactoring.
session[:uid] = auth['uid']
session[:provider] = auth['provider']
session[:name] = auth['info']['name']
session[:nickname] = auth['info']['nickname']
session[:website] = auth['info']['urls']['Website']
session[:description] = auth['info']['description']
session[:image] = auth['info']['image']
session[:email] = auth['info']['email']
session[:oauth_token] = auth['credentials']['token']
session[:oauth_secret] = auth['credentials']['secret']
# The username is checked to ensure it is unique, if it is not,
# the user is informed that they need to change it.
# Everyone is redirected to /users/new to confirm that they'd like
# to have their username.
if User.first :username => auth['info']['nickname']
flash[:error] = "Sorry, someone else has that username. Please pick another."
end

# The username is checked to ensure it is unique, if it is not,
# the user is informed that they need to change it.
# Everyone is redirected to /users/new to confirm that they'd like
# to have their username.
if User.first :username => auth['info']['nickname']
flash[:error] = "Sorry, someone else has that username. Please pick another."
redirect_to new_user_path
else
redirect_to "/login"
end

redirect_to new_user_path
return
end
end
Expand Down
14 changes: 12 additions & 2 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def new
# is your run-of-the-mill login procedure.
def create
u = User.find_by_case_insensitive_username(params[:username])
if u.nil?
if u.nil? && admin_info.can_create_user?
# Grab the domain for this author from the request url
params[:domain] = root_url

Expand All @@ -28,7 +28,17 @@ def create
@user.save
sign_in(@user)
flash[:notice] = "Thanks for signing up!"
redirect_to root_path

if User.count == 1
# Administration options are available to the first user
@user.admin = true
@user.save

redirect_to "/admin"
else
redirect_to root_path
end

return
else
@user.errors.add(:password, "can't be empty")
Expand Down
7 changes: 4 additions & 3 deletions app/controllers/updates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ def index
render :json => @updates.map{ |u| UpdateJsonDecorator.decorate(u) }
}
end

end

def timeline
@list_class = "friends"
render_index(current_user.timeline)
if current_user
@list_class = "friends"
render_index(current_user.timeline)
end
end

def replies
Expand Down
1 change: 1 addition & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def create
Authorization.create_from_session!(session, @user)

flash[:notice] = "Thanks! You're all signed up with #{@user.username} for your username."

sign_in(@user)
redirect_to root_path
else
Expand Down
6 changes: 6 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class User
key :email, String
key :email_confirmed, Boolean

key :admin, Boolean, :default => false

def admin?
self.admin
end

# RSA for salmon usage
key :private_key, String

Expand Down
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,8 @@
resources :subscriptions, :except => [:update]
match 'subscriptions/:id.atom', :to => "subscriptions#post_update", :via => :post
match 'subscriptions/:id.atom', :to => "subscriptions#show", :via => :get

# Admin
match '/admin', :to => "admin#update", :via => :put
match '/admin', :to => "admin#index"
end

0 comments on commit 4151f15

Please sign in to comment.