-
Notifications
You must be signed in to change notification settings - Fork 5
use zauth id's so usernames can be updated (and use zpi for profile images!) #220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
26c39f8
708d2a4
f44bc8c
cf4ac70
d9c3afd
9aa249c
c61c6a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,22 +10,18 @@ | |
| # remember_created_at :datetime | ||
| # admin :boolean default(FALSE) | ||
| # dagschotel_id :integer | ||
| # avatar_file_name :string | ||
| # avatar_content_type :string | ||
| # avatar_file_size :integer | ||
| # avatar_updated_at :datetime | ||
| # orders_count :integer default(0) | ||
| # koelkast :boolean default(FALSE) | ||
| # name :string | ||
| # private :boolean default(FALSE) | ||
| # frecency :integer default(0), not null | ||
| # quickpay_hidden :boolean default(FALSE) | ||
| # userkey :string | ||
| # zauth_id :integer | ||
| # | ||
|
|
||
| class User < ApplicationRecord | ||
| include FriendlyId | ||
| include Avatarable | ||
| include Statistics | ||
|
|
||
| friendly_id :name, use: :finders | ||
|
|
@@ -40,12 +36,28 @@ class User < ApplicationRecord | |
| scope :publik, -> { where private: false } | ||
|
|
||
| def self.from_omniauth(auth) | ||
| db_user = find_or_create_by!(name: auth.uid) do |user| | ||
| user.avatar = Paperclip.io_adapters.for(Identicon.data_url_for(auth.uid)) | ||
| user.generate_key! | ||
| user.private = true | ||
| zauth_id = auth.extra.raw_info["id"] | ||
| unless zauth_id.is_a? Integer | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this happen? |
||
| raise( | ||
| "zauth id is not valid, this is not good, what is happening? what did you do? i am confused and will give up" | ||
| ) | ||
|
|
||
| end | ||
|
|
||
| db_user = find_by(zauth_id: zauth_id) | ||
|
|
||
| if db_user.nil? | ||
| db_user = find_or_create_by!(name: auth.uid) do |user| | ||
| user.generate_key! | ||
| user.private = true | ||
| end | ||
|
|
||
| db_user.zauth_id = zauth_id | ||
| end | ||
|
|
||
| # overwrite name (for if name changed) | ||
| db_user.name = auth.uid | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An alternative here would be to just take some downtime and do a migration where we prepare a mapping of usernames to ZAuth id's, and fill them in the database. That way, the whole table will be filled, we can make the |
||
|
|
||
| # get roles info | ||
| roles = auth.dig(:extra, :raw_info, :roles) || [] | ||
|
|
||
|
|
@@ -107,12 +119,14 @@ def balance | |
| bal - total_pending | ||
| end | ||
|
|
||
| def avatar(*) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the (*) needed here? |
||
| "https://zpi.zeus.gent/image/#{zauth_id || 0}" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be a config option? @TomNaessens where in the config should you put functions?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, somewhere in the application.rb config at the end, there's some examples on production.rb already (e.g. Tab url) |
||
| end | ||
|
|
||
| # Static Users | ||
|
|
||
| def self.guest | ||
| @guest || find_or_create_by(name: "Guest") do |user| | ||
| user.avatar = File.new(File.join("app", "assets", "images", "guest.png")) | ||
| end | ||
| @guest || find_or_create_by(name: "Guest") | ||
| end | ||
|
|
||
| def guest? | ||
|
|
@@ -121,7 +135,6 @@ def guest? | |
|
|
||
| def self.koelkast | ||
| @koelkast || find_or_create_by(name: "Koelkast") do |user| | ||
| user.avatar = File.new(File.join("app", "assets", "images", "logo.png")) | ||
| user.koelkast = true | ||
| end | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| class RemoveAvatarFromUsers < ActiveRecord::Migration[8.1] | ||
| def change | ||
| remove_column :users, :avatar_file_name, :string | ||
| remove_column :users, :avatar_content_type, :string | ||
| remove_column :users, :avatar_file_size, :integer | ||
| remove_column :users, :avatar_updated_at, :datetime | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| class AddZauthIdToUsers < ActiveRecord::Migration[8.1] | ||
| def change | ||
| add_column :users, :zauth_id, :integer | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we're finding by zauth_id, having an index here would be nice. Together with https://github.com/ZeusWPI/Tap/pull/220/changes#r2622094045, we could even make this non nullable after doing the migration.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this get a unique constraint? Having two users with the same id would be strange and should throw some error. |
||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be cleaner to let the
uidin the omniauth block return thisid, release a new major version of the zeuswpi omniauth policy, and then use theextra.raw_info["name"]down below instead ofuid?It's a bit more work but if we have a unique id in Zauth to use as identification, we should probably migrate everything to it in the long run.