-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to restrict users to specific languages
- Loading branch information
Showing
17 changed files
with
302 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
module Alchemy | ||
class UserWithLanguages < SimpleDelegator | ||
alias_method :user, :__getobj__ | ||
|
||
def language_restriction_implemented? | ||
user.respond_to? :languages | ||
end | ||
|
||
def accessible_languages | ||
language_restriction_implemented? ? super : Alchemy::Language.all | ||
end | ||
|
||
def accessible_language_ids | ||
accessible_languages.pluck(:id) | ||
end | ||
|
||
def languages_restricted? | ||
!language_restriction_implemented? || | ||
accessible_languages != Alchemy::Language.all | ||
end | ||
|
||
def accessible_site_ids | ||
accessible_languages.map(&:site_id).uniq | ||
end | ||
|
||
def accessible_sites | ||
Alchemy::Site.where(id: accessible_site_ids).order(:id) | ||
end | ||
|
||
def can_access_language?(language) | ||
accessible_languages.where(id: language.id).any? | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
app/views/alchemy/admin/partials/_language_tree_select.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
db/migrate/20230407153522_create_alchemy_users_languages.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateAlchemyUsersLanguages < ActiveRecord::Migration[6.1] | ||
def change | ||
create_table :alchemy_users_languages do |t| | ||
t.bigint :user_id, null: false | ||
t.bigint :language_id, full: false | ||
|
||
t.index :user_id | ||
t.index :language_id | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
spec/dummy/db/migrate/20230419135736_create_alchemy_users_languages.alchemy.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
# This migration comes from alchemy (originally 20230407153522) | ||
class CreateAlchemyUsersLanguages < ActiveRecord::Migration[6.1] | ||
def change | ||
create_table :alchemy_users_languages do |t| | ||
t.bigint :user_id, null: false | ||
t.bigint :language_id, full: false | ||
|
||
t.index :user_id | ||
t.index :language_id | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.