Added welcome and profile actions to the my controller

This commit is contained in:
Christoph Haas 2017-04-23 13:37:48 +02:00
parent 8e4dd058b6
commit 020a864736
12 changed files with 125 additions and 19 deletions

View file

@ -0,0 +1,17 @@
# User profiles and screenshot upload management
class MyController < ApplicationController
def index
end
def uploads
# @current_users_screenshots gets filled in ApplicationController
#
# Get packages that the uploaded screenshots belong to.
@packages = Package.joins(:screenshots).where(
'screenshots.id' => @current_users_screenshots)
end
def welcome
end
end

View file

@ -113,4 +113,9 @@ class Screenshot < ApplicationRecord
def self.uploaded_by(token) def self.uploaded_by(token)
self.where(approved: false, uploaderhash: token) self.where(approved: false, uploaderhash: token)
end end
# Check whether the user has administrative permissions
def can_admin?
self.admin == 1
end
end end

View file

@ -1,4 +1,5 @@
class User < ApplicationRecord class User < ApplicationRecord
has_many :screenshots, :inverse_of=>:user
# Include default devise modules. Others available are: # Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable # :confirmable, :lockable, :timeoutable and :omniauthable
@ -18,6 +19,25 @@ class User < ApplicationRecord
:github :github
] ]
# Return a human-friendly string describing the user's SSO provider
def pretty_provider
case self.provider
when 'launchpad'
'Ubuntu One/Launchpad'
when 'stackexchange'
'StackExchange'
when 'google_oauth2'
'Google'
when 'amazon'
'Amazon'
when 'github'
'GitHub'
end
end
def is_admin?
self.admin == 1
end
def self.from_omniauth(auth) def self.from_omniauth(auth)
where(provider: auth.provider, email: auth.info.email).first_or_create do |user| where(provider: auth.provider, email: auth.info.email).first_or_create do |user|
@ -29,5 +49,4 @@ class User < ApplicationRecord
user.password = Devise.friendly_token[0,20] user.password = Devise.friendly_token[0,20]
end end
end end
end end

11
app/views/my/_menu.slim Normal file
View file

@ -0,0 +1,11 @@
.menu-centered
ul.menu.icon-top
li class=('active' if action_name=='profile')
= link_to my_profile_path
= fa_icon 'user-o 2x', text: 'Myself'
li
a href='#'
= fa_icon 'image 2x', text: 'My uploads'
li
= link_to destroy_user_session_path, :method => :delete
= fa_icon 'lock 2x', text: 'Logout'

31
app/views/my/profile.slim Normal file
View file

@ -0,0 +1,31 @@
.row
= render partial: 'menu'
h1 As far as we know
h2 Your name
= text_field_tag 'name', current_user.name
p.help-text
' Your name will be shown to others along with the images you upload.
' Feel free to change it.
h2 Your email address
p = current_user.email
p.help-text
' This web site recognizes you by your email address.
' Don't worry - it will not be shown or given to anyone.
h2 Single-sign-on provider
p You logged in using #{current_user.pretty_provider}.
h2 The first time you were here was
p = current_user.created_at.to_formatted_s(:long_ordinal)
h2 Number of screenshots you uploaded
p = current_user.screenshots.count
- if current_user.is_admin?
h2 Admin
p Apparently you are an administrator. Be careful with that thing!

View file

@ -1,6 +1,10 @@
.row .row
= render partial: 'menu'
.small-8.columns.small-centered .small-8.columns.small-centered
.callout.success
h1 Welcome h1 Welcome
- if current_user.provider == 'debian-sso' - if current_user.provider == 'debian-sso'

View file

@ -16,7 +16,7 @@ Rails.application.routes.draw do
get 'packages/list' => 'packages#list', as: :packages_list get 'packages/list' => 'packages#list', as: :packages_list
get 'moderate' => 'moderate#index' get 'moderate' => 'moderate#index'
get 'logs' => 'logs#index' get 'logs' => 'logs#index'
get 'my/index' get 'my/profile'
get 'my/uploads' get 'my/uploads'
get 'my/welcome' get 'my/welcome'
get 'package/:name' => 'packages#details', as: :package, name: /[^\/]+/ get 'package/:name' => 'packages#details', as: :package, name: /[^\/]+/

View file

@ -0,0 +1,7 @@
# Previous local accounts have NULL as its value for the 'provider'.
# Turn that into an explicit 'local' string.
class ChangeUsersDefaultProviderLocal < ActiveRecord::Migration[5.0]
def change
execute "update users set provider='local' where provider is null"
end
end

View file

@ -0,0 +1,5 @@
class AddAdminFieldToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :admin, :integer, default: 0
end
end

View file

@ -0,0 +1,5 @@
class AddScreenshotReferenceToUsers < ActiveRecord::Migration[5.0]
def change
add_column :screenshots, :user_id, :integer, default: 0
end
end

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170406155141) do ActiveRecord::Schema.define(version: 20170422183028) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -58,6 +58,7 @@ ActiveRecord::Schema.define(version: 20170406155141) do
t.integer "image_file_size" t.integer "image_file_size"
t.datetime "image_updated_at" t.datetime "image_updated_at"
t.string "image_fingerprint" t.string "image_fingerprint"
t.integer "user_id", default: 0
t.index ["id", "approved"], name: "id_approved", using: :btree t.index ["id", "approved"], name: "id_approved", using: :btree
t.index ["id", "uploaderhash"], name: "id_uploaderhash", using: :btree t.index ["id", "uploaderhash"], name: "id_uploaderhash", using: :btree
end end
@ -78,6 +79,7 @@ ActiveRecord::Schema.define(version: 20170406155141) do
t.datetime "locked_at" t.datetime "locked_at"
t.string "provider" t.string "provider"
t.string "uid" t.string "uid"
t.integer "admin", default: 0
t.index ["email", "provider"], name: "index_users_on_email_and_provider", unique: true, using: :btree t.index ["email", "provider"], name: "index_users_on_email_and_provider", unique: true, using: :btree
end end

View file

@ -95,7 +95,7 @@ Create a user for moderation:
bundle exec rails c -e production bundle exec rails c -e production
user = User.create(realname: 'Christoph Haas', password: 'foobartest', email: 'email@christoph-haas.de') user = User.create(realname: 'Christoph Haas', password: 'foobartest', email: 'email@christoph-haas.de', provider: 'local')
user.save! user.save!
Tidy up the screenshot data: Tidy up the screenshot data: