diff --git a/Gemfile b/Gemfile index ef9c0d78..838941bc 100644 --- a/Gemfile +++ b/Gemfile @@ -8,6 +8,7 @@ gem 'rails', '3.0.9' gem 'warden' gem 'devise', '1.3.4' +gem 'devise_cas_authenticatable', :git => 'git://github.com/Bushido/devise_cas_authenticatable.git' gem 'mongoid', '~> 2.0.2' gem 'bson_ext', '~> 1.3.0' @@ -38,6 +39,7 @@ gem 'delayed_job_mongoid', '1.0.2' gem 'rubyzip' gem 'locomotive_jammit-s3', :require => 'jammit-s3' gem 'SystemTimer', :platforms => :ruby_18 +gem 'cells' # The rest of the dependencies are for use when in the locomotive dev environment diff --git a/app/cells/admin/global_actions/show.html.haml b/app/cells/admin/global_actions/show.html.haml new file mode 100644 index 00000000..6d0f2e0f --- /dev/null +++ b/app/cells/admin/global_actions/show.html.haml @@ -0,0 +1,12 @@ +#global-actions-bar + - @list.each_with_index do |item, index| + + - i18n_options = item.delete(:i18n_options) + + - if index > 0 + %span= '|' + + - if i18n_options + != t(i18n_options[:key], i18n_options[:arg] => link_to(i18n_options[:value], item[:url])) + - else + = link_to item[:label], item[:url], item.delete_if { |k, v| %w(label url).include?(k) } \ No newline at end of file diff --git a/app/cells/admin/global_actions_cell.rb b/app/cells/admin/global_actions_cell.rb new file mode 100644 index 00000000..a0310f2f --- /dev/null +++ b/app/cells/admin/global_actions_cell.rb @@ -0,0 +1,34 @@ +class Admin::GlobalActionsCell < ::Admin::MenuCell + + attr_reader :current_admin, :current_site_url + + def show(args) + @current_admin = args[:current_admin] + @current_site_url = args[:current_site_url] + super + end + + protected + + def build_list + add :welcome, :url => edit_admin_my_account_url, :i18n_options => { + :key => 'admin.shared.header.welcome', + :arg => :name, + :value => @current_admin.name + } + + add :see, :url => current_site_url, :id => 'viewsite', :target => '_blank' + + if Locomotive.config.multi_sites? && current_admin.sites.size > 1 + add :switch, :url => '#', :id => 'sites-picker-link' + end + + add :help, :url => '#', :class => 'tutorial', :id => 'help' + add :logout, :url => destroy_admin_session_url, :confirm => t('admin.messages.confirm') + end + + def localize_label(label, options = {}) + I18n.t("admin.shared.header.#{label}", options) + end + +end diff --git a/app/cells/admin/main_menu/show.html.haml b/app/cells/admin/main_menu/show.html.haml new file mode 100644 index 00000000..9dde22a3 --- /dev/null +++ b/app/cells/admin/main_menu/show.html.haml @@ -0,0 +1,9 @@ +%ul#menu + - @list.each_with_index do |item, index| + - index += 1 + %li{ :class => "item #{'first' if index == 1} item-#{index} #{item[:class]}" } + %span + = link_to item[:url] do + %em   + %span= item[:label] + %li.clear \ No newline at end of file diff --git a/app/cells/admin/main_menu_cell.rb b/app/cells/admin/main_menu_cell.rb new file mode 100644 index 00000000..e5cc66db --- /dev/null +++ b/app/cells/admin/main_menu_cell.rb @@ -0,0 +1,10 @@ +class Admin::MainMenuCell < ::Admin::MenuCell + + protected + + def build_list + add :contents, :url => admin_pages_url + add :settings, :url => edit_admin_current_site_url + end + +end diff --git a/app/cells/admin/menu/show.html.haml b/app/cells/admin/menu/show.html.haml new file mode 100644 index 00000000..f6bfff70 --- /dev/null +++ b/app/cells/admin/menu/show.html.haml @@ -0,0 +1 @@ +%p To be overridden \ No newline at end of file diff --git a/app/cells/admin/menu_cell.rb b/app/cells/admin/menu_cell.rb new file mode 100644 index 00000000..ee8b0277 --- /dev/null +++ b/app/cells/admin/menu_cell.rb @@ -0,0 +1,95 @@ +class Admin::MenuCell < Cell::Base + + include ::Rails.application.routes.url_helpers + + delegate :sections, :to => :parent_controller + + attr_accessor :list + + def initialize(*args) + super + self.list = [] + end + + def show(args = {}) + self.build_list + render + end + + def url_options + super.reverse_merge( + :host => request.host_with_port, + :protocol => request.protocol, + :_path_segments => request.symbolized_path_parameters + ).merge(:script_name => request.script_name) + end + + class MenuProxy + + def initialize(cell) + @cell = cell + end + + def method_missing(meth, *args) + @cell.send(meth, *args) + end + + end + + def self.update_for(name, &block) + method_name = "build_list_with_#{name}".to_sym + previous_method_name = "build_list_without_#{name}".to_sym + + self.send(:define_method, method_name) do + self.send(previous_method_name) + block.call(MenuProxy.new(self)) + end + + alias_method_chain :build_list, name.to_sym + end + + protected + + def build_list + raise 'the build_list method must be overridden' + end + + def build_item(name, attributes) + unless attributes.key?(:label) + attributes[:label] = localize_label(name) + end + + attributes.merge!(:name => name, :class => name.to_s.dasherize.downcase) + end + + def add(name, attributes) + self.list << build_item(name, attributes) + end + + def add_after(pivot, name, attributes) + index = self.list.index { |i| i[:name] == pivot } + self.list.insert(index + 1, self.build_item(name, attributes)) + end + + def add_before(pivot, name, attributes) + index = self.list.index { |i| i[:name] == pivot } + self.list.insert(index, self.build_item(name, attributes)) + end + + def modify(name, attributes) + self.find(name).merge!(attributes) + end + + def remove(name) + self.list.delete_if { |i| i[:name] == name } + end + + def find(name) + self.list.detect { |i| i[:name] == name } + end + + def localize_label(label) + I18n.t("admin.shared.menu.#{label}") + end + +end diff --git a/app/cells/admin/settings_menu/show.html.haml b/app/cells/admin/settings_menu/show.html.haml new file mode 100644 index 00000000..1e0348d3 --- /dev/null +++ b/app/cells/admin/settings_menu/show.html.haml @@ -0,0 +1,4 @@ +- @list.each do |item| + %li + = link_to item[:url], :class => item[:class] do + %span= item[:label] diff --git a/app/cells/admin/settings_menu_cell.rb b/app/cells/admin/settings_menu_cell.rb new file mode 100644 index 00000000..30b6ae8d --- /dev/null +++ b/app/cells/admin/settings_menu_cell.rb @@ -0,0 +1,17 @@ +class Admin::SettingsMenuCell < ::Admin::MenuCell + + protected + + def build_list + add :site, :url => edit_admin_current_site_url + add :theme_assets, :url => admin_theme_assets_url + add :account, :url => edit_admin_my_account_url + end + + def build_item(name, attributes) + item = super + enhanced_class = "#{'on' if name.to_s == sections(:sub)} #{item[:class]}" + item.merge(:class => enhanced_class) + end + +end diff --git a/app/helpers/admin/base_helper.rb b/app/helpers/admin/base_helper.rb index fb01c1aa..b3f858c4 100644 --- a/app/helpers/admin/base_helper.rb +++ b/app/helpers/admin/base_helper.rb @@ -9,21 +9,7 @@ module Admin::BaseHelper end end - def admin_menu_item(name, url) - index = controller.instance_variable_get(:@menu_index) || 1 - controller.instance_variable_set(:@menu_index, index + 1) - label = content_tag(:em, escape_once(' ')) + content_tag(:span, t("admin.shared.menu.#{name}")) - content_tag(:li, content_tag(:span) + link_to(label, url), :class => "item #{'first' if index == 1} item-#{index} #{name.dasherize}") - end - - def admin_button_tag(text, url, options = {}) - text = text.is_a?(Symbol) ? t(".#{text}") : text - link_to(url, options) do - content_tag(:em, escape_once(' ')) + text - end - end - - def admin_submenu_item(name, url, options = {}, &block) + def admin_content_menu_item(name, url, options = {}, &block) default_options = { :i18n => true, :css => name.dasherize.downcase } default_options.merge!(options) @@ -39,6 +25,13 @@ module Admin::BaseHelper end end + def admin_button_tag(text, url, options = {}) + text = text.is_a?(Symbol) ? t(".#{text}") : text + link_to(url, options) do + content_tag(:em, escape_once(' ')) + text + end + end + def admin_item_toggler(object) image_tag("admin/list/icons/node_#{(cookies["folder-#{object._id}"] != 'none') ? 'open' : 'closed'}.png", :class => 'toggler') end diff --git a/app/helpers/admin/content_types_helper.rb b/app/helpers/admin/content_types_helper.rb index 8131876b..d51b3ee1 100644 --- a/app/helpers/admin/content_types_helper.rb +++ b/app/helpers/admin/content_types_helper.rb @@ -45,7 +45,7 @@ module Admin::ContentTypesHelper url = admin_contents_url(content_type.slug) css = @content_type && content_type.slug == @content_type.slug ? 'on' : '' - html = admin_submenu_item(label, url, :i18n => false, :css => css) do + html = admin_content_menu_item(label, url, :i18n => false, :css => css) do yield(content_type) end diff --git a/app/models/account.rb b/app/models/account.rb index 2d135d5c..5e0b6600 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -4,8 +4,7 @@ class Account include Locomotive::Mongoid::Document - # devise modules - devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :encryptable, :encryptor => :sha1 + devise *Locomotive.config.devise_modules ## attributes ## field :name diff --git a/app/views/admin/accounts/new.html.haml b/app/views/admin/accounts/new.html.haml index 4148a036..315f562a 100644 --- a/app/views/admin/accounts/new.html.haml +++ b/app/views/admin/accounts/new.html.haml @@ -1,7 +1,7 @@ - title t('.title') - content_for :submenu do - = render 'admin/shared/menu/settings' + = render_cell 'admin/settings_menu', :show %p!= t('.help') diff --git a/app/views/admin/current_site/edit.html.haml b/app/views/admin/current_site/edit.html.haml index c2cf7ea3..7e5f7143 100644 --- a/app/views/admin/current_site/edit.html.haml +++ b/app/views/admin/current_site/edit.html.haml @@ -1,7 +1,7 @@ - title link_to(@site.name.blank? ? @site.name_was : @site.name, '#', :rel => 'site_name', :title => t('.ask_for_name'), :class => 'editable') - content_for :submenu do - = render 'admin/shared/menu/settings' + = render_cell 'admin/settings_menu', :show - if can?(:manage, @site) - content_for :buttons do diff --git a/app/views/admin/import/new.html.haml b/app/views/admin/import/new.html.haml index e18e83aa..84b6752e 100644 --- a/app/views/admin/import/new.html.haml +++ b/app/views/admin/import/new.html.haml @@ -1,7 +1,7 @@ - title t('.title') - content_for :submenu do - = render 'admin/shared/menu/settings' + = render_cell 'admin/settings_menu', :show %p!= t('.help') diff --git a/app/views/admin/import/show.html.haml b/app/views/admin/import/show.html.haml index 8873a366..c07e8a14 100644 --- a/app/views/admin/import/show.html.haml +++ b/app/views/admin/import/show.html.haml @@ -4,7 +4,7 @@ - title t('.title') - content_for :submenu do - = render 'admin/shared/menu/settings' + = render_cell 'admin/settings_menu', :show %p!= t('.help') diff --git a/app/views/admin/memberships/new.html.haml b/app/views/admin/memberships/new.html.haml index 1ca4b282..024e6a35 100644 --- a/app/views/admin/memberships/new.html.haml +++ b/app/views/admin/memberships/new.html.haml @@ -1,7 +1,7 @@ - title t('.title') - content_for :submenu do - = render 'admin/shared/menu/settings' + = render_cell 'admin/settings_menu', :show %p!= t('.help') diff --git a/app/views/admin/my_account/edit.html.haml b/app/views/admin/my_account/edit.html.haml index f3259b8c..1755b3e0 100644 --- a/app/views/admin/my_account/edit.html.haml +++ b/app/views/admin/my_account/edit.html.haml @@ -4,7 +4,7 @@ = include_javascripts :account - content_for :submenu do - = render 'admin/shared/menu/settings' + = render_cell 'admin/settings_menu', :show - if multi_sites? - content_for :buttons do diff --git a/app/views/admin/shared/_header.html.haml b/app/views/admin/shared/_header.html.haml index 097c3f4b..586b578c 100644 --- a/app/views/admin/shared/_header.html.haml +++ b/app/views/admin/shared/_header.html.haml @@ -1,18 +1,7 @@ %h1 = link_to current_site.name, admin_pages_url, :class => 'single' -#global-actions-bar - != t('.welcome', :name => link_to(current_admin.name, edit_admin_my_account_url)) - %span= '|' - = link_to t('.see'), current_site_url, :id => 'viewsite', :target => '_blank' - - if multi_sites? && current_admin.sites.size > 1 - %span= '|' - = link_to t('.switch'), '#', :id => 'sites-picker-link' - %span= '|' - = link_to 'Help', '#', :class => 'tutorial', :id => 'help' - %span= '|' - = link_to t('.logout'), destroy_admin_session_url, :confirm => t('admin.messages.confirm') - += render_cell 'admin/global_actions', :show, :current_admin => current_admin - if multi_sites? && current_admin.sites.size > 1 #sites-picker{ :style => 'display: none' } diff --git a/app/views/admin/shared/_menu.html.haml b/app/views/admin/shared/_menu.html.haml deleted file mode 100644 index 19237f62..00000000 --- a/app/views/admin/shared/_menu.html.haml +++ /dev/null @@ -1,4 +0,0 @@ -%ul#menu - = admin_menu_item('contents', admin_pages_url) - = admin_menu_item('settings', edit_admin_current_site_url) - %li.clear diff --git a/app/views/admin/shared/menu/_contents.html.haml b/app/views/admin/shared/menu/_contents.html.haml index 490e0c9c..51dff9e2 100644 --- a/app/views/admin/shared/menu/_contents.html.haml +++ b/app/views/admin/shared/menu/_contents.html.haml @@ -1,4 +1,4 @@ -= admin_submenu_item 'pages', admin_pages_url do += admin_content_menu_item 'pages', admin_pages_url do - if can? :manage, @page .header %p= link_to t('admin.pages.index.new'), new_admin_page_url diff --git a/app/views/admin/shared/menu/_settings.html.haml b/app/views/admin/shared/menu/_settings.html.haml deleted file mode 100644 index cf99c43a..00000000 --- a/app/views/admin/shared/menu/_settings.html.haml +++ /dev/null @@ -1,4 +0,0 @@ -= admin_submenu_item 'site', edit_admin_current_site_url -/ = admin_submenu_item 'snippets', admin_snippets_url -= admin_submenu_item 'theme_assets', admin_theme_assets_url -= admin_submenu_item 'account', edit_admin_my_account_url \ No newline at end of file diff --git a/app/views/admin/sites/new.html.haml b/app/views/admin/sites/new.html.haml index fc1b3bf1..c4564ed2 100644 --- a/app/views/admin/sites/new.html.haml +++ b/app/views/admin/sites/new.html.haml @@ -1,7 +1,7 @@ - title t('.title') - content_for :submenu do - = render 'admin/shared/menu/settings' + = render_cell 'admin/settings_menu', :show %p!= t('.help') diff --git a/app/views/admin/snippets/edit.html.haml b/app/views/admin/snippets/edit.html.haml index 626b5b50..7afd277e 100644 --- a/app/views/admin/snippets/edit.html.haml +++ b/app/views/admin/snippets/edit.html.haml @@ -1,7 +1,7 @@ - title t('.title') - content_for :submenu do - = render 'admin/shared/menu/settings' + = render_cell 'admin/settings_menu', :show - content_for :buttons do = admin_button_tag t('admin.snippets.index.new'), new_admin_snippet_url, :class => 'new' diff --git a/app/views/admin/snippets/new.html.haml b/app/views/admin/snippets/new.html.haml index add05e7a..f589f1ff 100644 --- a/app/views/admin/snippets/new.html.haml +++ b/app/views/admin/snippets/new.html.haml @@ -1,7 +1,7 @@ - title t('.title') - content_for :submenu do - = render 'admin/shared/menu/settings' + = render_cell 'admin/settings_menu', :show %p!= t('.help') diff --git a/app/views/admin/theme_assets/edit.html.haml b/app/views/admin/theme_assets/edit.html.haml index 9258792a..3165d11d 100644 --- a/app/views/admin/theme_assets/edit.html.haml +++ b/app/views/admin/theme_assets/edit.html.haml @@ -1,7 +1,7 @@ - title t('.title', :file => @theme_asset.source_filename) - content_for :submenu do - = render 'admin/shared/menu/settings' + = render_cell 'admin/settings_menu', :show - if can?(:manage, ThemeAsset) - content_for :buttons do diff --git a/app/views/admin/theme_assets/index.html.haml b/app/views/admin/theme_assets/index.html.haml index ff8deed0..0d311558 100644 --- a/app/views/admin/theme_assets/index.html.haml +++ b/app/views/admin/theme_assets/index.html.haml @@ -1,7 +1,7 @@ - title t('.title') - content_for :submenu do - = render 'admin/shared/menu/settings' + = render_cell 'admin/settings_menu', :show - content_for :buttons do = admin_button_tag t('admin.snippets.index.new'), new_admin_snippet_url, :class => 'new' if can?(:manage, Snippet) diff --git a/app/views/admin/theme_assets/new.html.haml b/app/views/admin/theme_assets/new.html.haml index 3b4024e5..91819d29 100644 --- a/app/views/admin/theme_assets/new.html.haml +++ b/app/views/admin/theme_assets/new.html.haml @@ -1,7 +1,7 @@ - title t('.title') - content_for :submenu do - = render 'admin/shared/menu/settings' + = render_cell 'admin/settings_menu', :show %p!= t('.help') diff --git a/config/application.rb b/config/application.rb index cf435b9c..fe4c8dd6 100644 --- a/config/application.rb +++ b/config/application.rb @@ -16,7 +16,7 @@ module Locomotive # Add additional load paths for your own custom dirs # config.load_paths += %W( #{config.root}/extras ) - config.autoload_paths += %W( #{config.root}/app/models/extensions #{config.root}/app/models/extensions/site #{config.root}/app/models/extensions/page #{config.root}/app/models/extensions/asset) + config.autoload_paths += %W( #{config.root}/app/models/extensions #{config.root}/app/models/extensions/site #{config.root}/app/models/extensions/page #{config.root}/app/models/extensions/asset #{config.root}/app/cells/admin) # Only load the plugins named here, in the order given (default is alphabetical). # :all can be used as a placeholder for all plugins not explicitly named diff --git a/config/locales/admin_ui.en.yml b/config/locales/admin_ui.en.yml index 6db16514..88f43308 100644 --- a/config/locales/admin_ui.en.yml +++ b/config/locales/admin_ui.en.yml @@ -16,6 +16,7 @@ en: welcome: Welcome, %{name} see: See website switch: Switch to another site + help: Help logout: Log out menu: contents: Contents diff --git a/config/locales/admin_ui.fr.yml b/config/locales/admin_ui.fr.yml index b21752dc..d3f3bf65 100644 --- a/config/locales/admin_ui.fr.yml +++ b/config/locales/admin_ui.fr.yml @@ -26,6 +26,7 @@ fr: welcome: "Bonjour, %{name}" see: Voir le site web switch: Passer à un autre site + help: Aide logout: Se déconnecter menu: contents: Contenu diff --git a/config/mongoid.yml b/config/mongoid.yml index 8b08b667..b81cd544 100644 --- a/config/mongoid.yml +++ b/config/mongoid.yml @@ -9,7 +9,8 @@ defaults: &defaults development: <<: *defaults - database: locomotive_dev + # database: locomotive_dev + database: locomotive_cas_dev test: <<: *defaults diff --git a/lib/locomotive/configuration.rb b/lib/locomotive/configuration.rb index 9dc4f361..f890a99f 100644 --- a/lib/locomotive/configuration.rb +++ b/lib/locomotive/configuration.rb @@ -21,7 +21,8 @@ module Locomotive :verbose => true, :metastore => URI.encode("file:#{Rails.root}/tmp/dragonfly/cache/meta"), # URI encoded in case of spaces :entitystore => URI.encode("file:#{Rails.root}/tmp/dragonfly/cache/body") - } + }, + :devise_modules => [:database_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :encryptable, { :encryptor => :sha1 }] } cattr_accessor :settings diff --git a/lib/locomotive/hosting/bushido/enabler.rb b/lib/locomotive/hosting/bushido/enabler.rb index c4108757..c995259c 100644 --- a/lib/locomotive/hosting/bushido/enabler.rb +++ b/lib/locomotive/hosting/bushido/enabler.rb @@ -22,26 +22,44 @@ module Locomotive module ClassMethods def bushido_app_claimed? - ENV['BUSHIDO_CLAIMED'].present? && ENV['BUSHIDO_CLAIMED'].to_s.downcase == 'true' + ENV['BUSHIDO_CLAIMED'].present? && Boolean.set(ENV['BUSHIDO_CLAIMED']) end def enable_bushido! self.config.domain = ENV['APP_TLD'] unless self.config.multi_sites? + self.config.devise_modules = [:cas_authenticatable, :rememberable, :trackable] + self.enhance_models self.disable_authentication_for_not_claimed_app + self.setup_cas_client + self.setup_smtp_settings self.add_middlewares + self.tweak_ui + self.config.delayed_job = true # force to use delayed_job self.bushido_domains = ::Bushido::App.domains self.bushido_subdomain = ::Bushido::App.subdomain end + def tweak_ui + edit_account_url = 'https://auth.bushi.do/users/edit' + + ::Admin::GlobalActionsCell.update_for(:bushido) do |menu| + menu.modify :welcome, :url => edit_account_url + end + + ::Admin::SettingsMenuCell.update_for(:bushido) do |menu| + menu.modify :account, :url => edit_account_url + end + end + def enhance_models Site.send :include, Locomotive::Hosting::Bushido::CustomDomain Site.send :include, Locomotive::Hosting::Bushido::FirstInstallation @@ -52,6 +70,19 @@ module Locomotive Admin::BaseController.send :include, Locomotive::Hosting::Bushido::Devise end + def setup_cas_client + ::Devise.setup do |config| + config.cas_base_url = 'https://auth.bushi.do/cas' + config.cas_logout_url = 'https://auth.bushi.do/cas/logout' + end + + Admin::SessionsController.class_eval do + def new + redirect_to admin_pages_url + end + end + end + def setup_smtp_settings ActionMailer::Base.delivery_method = :smtp ActionMailer::Base.smtp_settings = { diff --git a/locomotive_cms.gemspec b/locomotive_cms.gemspec index b052c133..f6ac7c72 100644 --- a/locomotive_cms.gemspec +++ b/locomotive_cms.gemspec @@ -30,6 +30,7 @@ Gem::Specification.new do |s| s.add_dependency "locomotive_liquid", "2.2.2" s.add_dependency "formtastic", "~> 1.2.3" s.add_dependency "inherited_resources", "~> 1.1.2" + s.add_dependency "cells" s.add_dependency "json_pure", "1.5.1" s.add_dependency "bushido"