From 020a864736bd0ad741487781f3bd741b19ef4e52 Mon Sep 17 00:00:00 2001 From: Christoph Haas Date: Sun, 23 Apr 2017 13:37:48 +0200 Subject: [PATCH] Added welcome and profile actions to the my controller --- app/controllers/my_controller.rb | 17 ++++++++++ app/models/screenshot.rb | 5 +++ app/models/user.rb | 21 +++++++++++- app/views/my/_menu.slim | 11 ++++++ app/views/my/profile.slim | 31 +++++++++++++++++ app/views/my/welcome.slim | 34 +++++++++++-------- config/routes.rb | 2 +- ...908_change_users_default_provider_local.rb | 7 ++++ ...20170422182228_add_admin_field_to_users.rb | 5 +++ ...83028_add_screenshot_reference_to_users.rb | 5 +++ db/schema.rb | 4 ++- doc/README.md | 2 +- 12 files changed, 125 insertions(+), 19 deletions(-) create mode 100644 app/controllers/my_controller.rb create mode 100644 app/views/my/_menu.slim create mode 100644 app/views/my/profile.slim create mode 100644 db/migrate/20170422181908_change_users_default_provider_local.rb create mode 100644 db/migrate/20170422182228_add_admin_field_to_users.rb create mode 100644 db/migrate/20170422183028_add_screenshot_reference_to_users.rb diff --git a/app/controllers/my_controller.rb b/app/controllers/my_controller.rb new file mode 100644 index 0000000..c2ad542 --- /dev/null +++ b/app/controllers/my_controller.rb @@ -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 diff --git a/app/models/screenshot.rb b/app/models/screenshot.rb index a98c29f..024e731 100644 --- a/app/models/screenshot.rb +++ b/app/models/screenshot.rb @@ -113,4 +113,9 @@ class Screenshot < ApplicationRecord def self.uploaded_by(token) self.where(approved: false, uploaderhash: token) end + + # Check whether the user has administrative permissions + def can_admin? + self.admin == 1 + end end diff --git a/app/models/user.rb b/app/models/user.rb index b7bb82e..4e3b7f0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,5 @@ class User < ApplicationRecord + has_many :screenshots, :inverse_of=>:user # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable @@ -18,6 +19,25 @@ class User < ApplicationRecord :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) 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] end end - end diff --git a/app/views/my/_menu.slim b/app/views/my/_menu.slim new file mode 100644 index 0000000..3da6f50 --- /dev/null +++ b/app/views/my/_menu.slim @@ -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' diff --git a/app/views/my/profile.slim b/app/views/my/profile.slim new file mode 100644 index 0000000..739e1c1 --- /dev/null +++ b/app/views/my/profile.slim @@ -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! diff --git a/app/views/my/welcome.slim b/app/views/my/welcome.slim index 512541e..031680c 100644 --- a/app/views/my/welcome.slim +++ b/app/views/my/welcome.slim @@ -1,20 +1,24 @@ .row + + = render partial: 'menu' + .small-8.columns.small-centered - h1 Welcome + .callout.success + h1 Welcome - - if current_user.provider == 'debian-sso' - p - 'You have just logged in using your Debian SSO certificate which proves that - 'you are associated to the Debian project. So you are invited to upload any - 'screenshots. Your uploads will instantly be published and visible in applications - 'and web sites that use this service. - - else - p - 'You are now logged in. We will associate all your future uploads of screenshots - 'with your account. So others can see what you contributed to this site. + - if current_user.provider == 'debian-sso' + p + 'You have just logged in using your Debian SSO certificate which proves that + 'you are associated to the Debian project. So you are invited to upload any + 'screenshots. Your uploads will instantly be published and visible in applications + 'and web sites that use this service. + - else + p + 'You are now logged in. We will associate all your future uploads of screenshots + 'with your account. So others can see what you contributed to this site. - p - ' If you want to help then check out the - a href="/packages?show=without" packages that have no screenshots - ' yet. + p + ' If you want to help then check out the + a href="/packages?show=without" packages that have no screenshots + ' yet. diff --git a/config/routes.rb b/config/routes.rb index 51062ac..40fd28b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -16,7 +16,7 @@ Rails.application.routes.draw do get 'packages/list' => 'packages#list', as: :packages_list get 'moderate' => 'moderate#index' get 'logs' => 'logs#index' - get 'my/index' + get 'my/profile' get 'my/uploads' get 'my/welcome' get 'package/:name' => 'packages#details', as: :package, name: /[^\/]+/ diff --git a/db/migrate/20170422181908_change_users_default_provider_local.rb b/db/migrate/20170422181908_change_users_default_provider_local.rb new file mode 100644 index 0000000..f446743 --- /dev/null +++ b/db/migrate/20170422181908_change_users_default_provider_local.rb @@ -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 diff --git a/db/migrate/20170422182228_add_admin_field_to_users.rb b/db/migrate/20170422182228_add_admin_field_to_users.rb new file mode 100644 index 0000000..92a7039 --- /dev/null +++ b/db/migrate/20170422182228_add_admin_field_to_users.rb @@ -0,0 +1,5 @@ +class AddAdminFieldToUsers < ActiveRecord::Migration[5.0] + def change + add_column :users, :admin, :integer, default: 0 + end +end diff --git a/db/migrate/20170422183028_add_screenshot_reference_to_users.rb b/db/migrate/20170422183028_add_screenshot_reference_to_users.rb new file mode 100644 index 0000000..f63a2d9 --- /dev/null +++ b/db/migrate/20170422183028_add_screenshot_reference_to_users.rb @@ -0,0 +1,5 @@ +class AddScreenshotReferenceToUsers < ActiveRecord::Migration[5.0] + def change + add_column :screenshots, :user_id, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index a309de2..5bed4e2 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.define(version: 20170406155141) do +ActiveRecord::Schema.define(version: 20170422183028) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -58,6 +58,7 @@ ActiveRecord::Schema.define(version: 20170406155141) do t.integer "image_file_size" t.datetime "image_updated_at" t.string "image_fingerprint" + t.integer "user_id", default: 0 t.index ["id", "approved"], name: "id_approved", using: :btree t.index ["id", "uploaderhash"], name: "id_uploaderhash", using: :btree end @@ -78,6 +79,7 @@ ActiveRecord::Schema.define(version: 20170406155141) do t.datetime "locked_at" t.string "provider" t.string "uid" + t.integer "admin", default: 0 t.index ["email", "provider"], name: "index_users_on_email_and_provider", unique: true, using: :btree end diff --git a/doc/README.md b/doc/README.md index b011d34..344397d 100644 --- a/doc/README.md +++ b/doc/README.md @@ -95,7 +95,7 @@ Create a user for moderation: 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! Tidy up the screenshot data: