diff --git a/Gemfile b/Gemfile index d2d88c5..0876fbc 100644 --- a/Gemfile +++ b/Gemfile @@ -20,7 +20,7 @@ gem "acts_as_unvlogable" gem 'youtube_it' gem 'gotcha' gem "geocoder" - +gem 'httparty' #database gem 'mini_magick' gem 'mongoid', '> 2.1', '< 3.0.0' diff --git a/app/controllers/admin/module_store_controller.rb b/app/controllers/admin/module_store_controller.rb index 8c5674d..ced1adf 100644 --- a/app/controllers/admin/module_store_controller.rb +++ b/app/controllers/admin/module_store_controller.rb @@ -1,6 +1,5 @@ class Admin::ModuleStoreController < OrbitBackendController - - @@store = STORE_CONFIG[:store_settings]["url"] + before_filter :check_central_server_connection, :only => [:get_extensions] def index @extensions = get_extensions @@ -85,7 +84,7 @@ class Admin::ModuleStoreController < OrbitBackendController protected def get_extensions - extensions = JSON.parse(open("#{@@store}/api/extensions").read) + extensions = store_session.extensions exist_exts = [] ext_file = File.new("#{Rails.root}/downloaded_extensions.rb", "r") @@ -112,7 +111,7 @@ class Admin::ModuleStoreController < OrbitBackendController end def get_extension(id) - JSON.parse(open("#{@@store}/api/extensions/#{id}").read) + store_session.get_extension(id) end def get_downloaded_extension @@ -142,4 +141,10 @@ class Admin::ModuleStoreController < OrbitBackendController end end + private + + def get_site + @site ||= Site.first + end + end \ No newline at end of file diff --git a/app/controllers/admin/omniauth_callbacks_controller.rb b/app/controllers/admin/omniauth_callbacks_controller.rb new file mode 100644 index 0000000..f034740 --- /dev/null +++ b/app/controllers/admin/omniauth_callbacks_controller.rb @@ -0,0 +1,10 @@ +class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController + def doorkeeper + oauth_data = request.env["omniauth.auth"] + @user = User.find_or_create_for_doorkeeper_oauth(oauth_data) + @user.update_doorkeeper_credentials(oauth_data) + @user.save + + sign_in_and_redirect @user + end +end \ No newline at end of file diff --git a/app/controllers/admin/sites_controller.rb b/app/controllers/admin/sites_controller.rb index 2e492dd..a3b3384 100644 --- a/app/controllers/admin/sites_controller.rb +++ b/app/controllers/admin/sites_controller.rb @@ -147,6 +147,11 @@ class Admin::SitesController < OrbitBackendController render :text => "success" end + def register_site + @site.register_site + redirect_to admin_module_store_path + end + protected def update_design(design) diff --git a/app/controllers/admin/template_store_controller.rb b/app/controllers/admin/template_store_controller.rb index 5f2344c..299b96c 100644 --- a/app/controllers/admin/template_store_controller.rb +++ b/app/controllers/admin/template_store_controller.rb @@ -3,24 +3,22 @@ require 'uri' require 'fileutils' require 'zip/zip' class Admin::TemplateStoreController < OrbitBackendController - + before_filter :check_central_server_connection, :only => [:get_templates] + before_filter :set_store - @@store_domain = STORE_CONFIG[:store_settings]["url"] def index - @store = @@store_domain @design_ids = Design.all.map{|d| d.template_store_id} - @templates = JSON.parse(get_templates) + @templates = get_templates.parsed_response render :layout => false end def show - @store = @@store_domain - @design_ids = Design.all.map{|d| d.template_store_id} - @template = JSON.parse(get_template(params[:id])) rescue nil + @design_ids = Design.all.map{|d| d.template_store_id} + @template = get_template(params[:id]).parsed_response rescue nil end def download_theme - url = @@store_domain + params["url"] + url = @store_url + params["url"] url_base = url.split('/')[2] url_path = '/'+url.split('/')[3..-1].join('/') Net::HTTP.start(url_base) do |http| @@ -32,7 +30,7 @@ class Admin::TemplateStoreController < OrbitBackendController end upload_package("#{params['slug']}.zip", params["id"]) File.delete("public/#{params['slug']}.zip") - render :json => {"success"=>true,"url"=>@@store_domain + params["url"]}.to_json + render :json => {"success"=>true,"url"=>@store_url + params["url"]}.to_json end protected @@ -92,19 +90,15 @@ class Admin::TemplateStoreController < OrbitBackendController end def get_template(id) - uri = URI.parse("#{@@store_domain}/api/templates/#{id}") - http = Net::HTTP.new(uri.host, uri.port) - request = Net::HTTP::Get.new(uri.request_uri) - response = http.request(request) - response.body + store_session.get_template(id) end def get_templates - uri = URI.parse("#{@@store_domain}/api/templates") - http = Net::HTTP.new(uri.host, uri.port) - request = Net::HTTP::Get.new(uri.request_uri) - response = http.request(request) - response.body + store_session.templates + end + + def set_store + @store_url = STORE_CONFIG[:store_settings]["url"] end end \ No newline at end of file diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c8fcfe2..98618ef 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -479,4 +479,18 @@ class ApplicationController < ActionController::Base end end + def check_central_server_connection + if @site.site_token? + flash[:notice]="Connected to the Store" + else + redirect_to admin_register_site_index_path + flash[:notice]="To Access the Store Please Connect It" + end + end + + def store_session + api_key = STORE_CONFIG[:store_settings]["api_key"] + @store = Store.new(@site.id.to_s,@site.site_token,api_key) if @site.site_token + end + end diff --git a/app/controllers/orbit_backend_controller.rb b/app/controllers/orbit_backend_controller.rb index 3a6a759..2759089 100644 --- a/app/controllers/orbit_backend_controller.rb +++ b/app/controllers/orbit_backend_controller.rb @@ -1,6 +1,7 @@ class OrbitBackendController < ApplicationController include OrbitCategory::Categorizing include OrbitCoreLib::Authorization + include OrbitCoreLib::PermissionUtility include OrbitTag::Tagging include AdminHelper include ApplicationHelper diff --git a/app/helpers/orbit_backend_helper.rb b/app/helpers/orbit_backend_helper.rb index ba2a87e..c946456 100644 --- a/app/helpers/orbit_backend_helper.rb +++ b/app/helpers/orbit_backend_helper.rb @@ -12,13 +12,13 @@ module OrbitBackendHelper def show_form_status_field(object) #by_object = (!object.is_expired? and object.is_pending?) - by_user = ((object.category.authed_users("approval_#{@module_app.key}").include?(current_user) rescue nil) or is_manager? or is_admin? or is_sub_manager?) + by_user = ((object.category.user_is_authorized_by_title?(current_user,"category_approval_#{@module_app.key}") rescue nil) or is_manager? or is_admin?) by_user end def show_approval_link(object) by_object = (!object.is_expired? and object.is_pending?) - by_user = ((object.category.authed_users("approval_#{@module_app.key}").include?(current_user) rescue nil) or is_manager? or is_admin? or is_sub_manager?) + by_user = ((object.category.user_is_authorized_by_title?(current_user,"category_approval_#{@module_app.key}") rescue nil) or is_manager? or is_admin?) by_object and by_user end @@ -306,7 +306,7 @@ module OrbitBackendHelper content_tag :li, link_to(t(quick[:translation] || :authorization_), eval("#{quick[:link]}"), class: "preview_trigger #{quick[:class]}") end when 'edit' - if authorization && approvable + if authorization && approvable || is_manager? content_tag :li, link_to(t(quick[:translation] || :edit), quick[:link].nil? ? '#' : eval("#{quick[:link]}('#{object.id}'#{link_option})"), class: quick[:class], data: eval("#{quick[:data]}")) end when 'delete' diff --git a/app/models/module_app.rb b/app/models/module_app.rb index 61673ec..9a5cb37 100644 --- a/app/models/module_app.rb +++ b/app/models/module_app.rb @@ -194,6 +194,7 @@ class ModuleApp # authorization def update_auth_approval_users user_ids = self.auth_approvals.inject([]) do |users, auth| + auth = auth.class.find(auth.id) users += auth.authorized_users.map{|user| user.id} end self.update_attribute(:auth_approval_users, user_ids.uniq) diff --git a/app/models/site.rb b/app/models/site.rb index a37cbbb..d1cfe57 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -46,14 +46,15 @@ class Site field :address field :phone_number, :type => Array,:default=>[] field :mobile_bar_color, :type => Array, :default=>[] - - + field :site_token field :mobile_on, :type => Boolean, :default => false belongs_to :design has_many :site_metas, :autosave => true, :dependent => :destroy validate :in_use_locales, :minimum_enabled_locales + index({ access_token: 1}, { unique: true }) + def minimum_enabled_locales size = self.in_use_locales.length if size < 1 @@ -95,5 +96,19 @@ class Site fetch_meta.save end end + + def generate_site_token + if self.site_token.nil? + self.site_token = SecureRandom.uuid.gsub('-','') + self.save + end + end + + def register_site + api_key = STORE_CONFIG[:store_settings]["api_key"] + self.generate_site_token + store = Store.new(self.id.to_s,self.site_token,api_key) + store.post_client(self.id.to_s,self.site_token,self.name) + end end diff --git a/app/views/admin/module_store/index.html.erb b/app/views/admin/module_store/index.html.erb index 2272c26..d9d10eb 100644 --- a/app/views/admin/module_store/index.html.erb +++ b/app/views/admin/module_store/index.html.erb @@ -184,7 +184,6 @@
<%= t(:module_store) %>
- diff --git a/app/views/admin/template_store/_template.html.erb b/app/views/admin/template_store/_template.html.erb index 970e138..bbeaa25 100644 --- a/app/views/admin/template_store/_template.html.erb +++ b/app/views/admin/template_store/_template.html.erb @@ -1,7 +1,7 @@ + <%= render :partial => 'template', :collection => @templates %> diff --git a/app/views/admin/template_store/show.html.erb b/app/views/admin/template_store/show.html.erb index c730b85..fd13d80 100644 --- a/app/views/admin/template_store/show.html.erb +++ b/app/views/admin/template_store/show.html.erb @@ -6,7 +6,7 @@
- <%= image_tag "#{@store}#{@template['preview']['preview']['thumb']['url']}", :class => "item-thumb" %> + <%= image_tag "#{@store_url}/#{@template['preview']['preview']['thumb']['url']}", :class => "item-thumb" %>

<%= @template['title'] %>

<% if @design_ids.include?(@template["_id"]["$oid"]) %> <%= link_to "Installed", "javascript:void(0);", "data-url" => @template['template']['template']['url'], :class=> 'btn btn-success download-link', "disabled"=>"disabled", "data-name"=>@template['title'], "data-slug"=>@template["_slugs"][0], "data-id"=>@template["_id"]["$oid"] %> diff --git a/config/initializers/redis.rb b/config/initializers/redis.rb new file mode 100644 index 0000000..c77dfe9 --- /dev/null +++ b/config/initializers/redis.rb @@ -0,0 +1,11 @@ +if defined?(PhusionPassenger) + PhusionPassenger.on_event(:starting_worker_process) do |forked| + if forked + Resque.redis.client.disconnect + Resque.redis = Redis.new(:host => 'localhost', :port => 6379) + Resque.redis.namespace = Site.first.resque_namespace rescue APP_CONFIG['orbit'] + else + # We're in conservative spawning mode. We don't need to do anything. + end + end +end \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index c433e63..4ce4512 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -229,11 +229,13 @@ en: groups: Groups help: Help hidden: Hidden + is_hidden: Hidden hide: Hide hits: Hits homepage: Homepage horizontal: Horizontal hot: Hot + is_hot: Hot image: Image images: Images info: Information @@ -352,11 +354,13 @@ en: public_r_tag: System Widget text: Text Area passed: Approved + is_checked: Approved password: Password password_change: Change password password_confirmation: Password confirmation password_current: Current password pending: Pending + is_pending: Pending personal_plugins: author : "Author" edit_brief_intro : "Edit Brief Intro." @@ -400,6 +404,7 @@ en: register: Register registered: Registered rejected: Rejected + is_rejected: Rejected rejected_reason: 'Reason:' rejected_reason_empty: "Approval rejected, no referencable information" related_links: Related Links @@ -527,6 +532,7 @@ en: to_search: Set as Search Key to_show: Display in frontend top: Top + is_top: Top total_visitors: Total Visitors traffic: Traffic type: Field Type diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index b412f5f..69682d2 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -355,11 +355,13 @@ zh_tw: public_r_tag: 系統模塊 text: 文字區域 passed: 通過審核 + is_checked: 通過審核 password: 密碼 password_change: 更改密碼 password_confirmation: 確認密碼 password_current: 目前的密碼 pending: 待審核 + is_pending: 待審核 personal_plugins: author : "著作人" edit_brief_intro : "編輯摘要" @@ -402,6 +404,7 @@ zh_tw: register: 註冊 registered: 已註冊 rejected: 拒絕 + is_rejected: 拒絕 rejected_reason: 拒絕原因:' rejected_reason_empty: "拒絕核准, 沒有參考資訊" related_links: 相關連結 diff --git a/config/routes.rb b/config/routes.rb index 573e814..af22d24 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -220,6 +220,7 @@ Orbit::Application.routes.draw do get 'update_orbit' get 'restart_server' end + match 'sites/register_site' => "sites#register_site" resources :tags do collection do diff --git a/config/store_config.yml b/config/store_config.yml index cd0fd3b..e086195 100644 --- a/config/store_config.yml +++ b/config/store_config.yml @@ -1,2 +1,4 @@ store_settings: url: "http://store.tp.rulingcom.com" + api_url: "http://store.tp.rulingcom.com/api" + api_key: 'Token token="2870f77e59168dbe3fbdffba466c7c8d"' diff --git a/lib/omniauth/strategies/doorkeeper.rb b/lib/omniauth/strategies/doorkeeper.rb new file mode 100644 index 0000000..16bdb93 --- /dev/null +++ b/lib/omniauth/strategies/doorkeeper.rb @@ -0,0 +1,26 @@ +module OmniAuth + module Strategies + class Doorkeeper < OmniAuth::Strategies::OAuth2 + option :name, :doorkeeper + + option :client_options, { + :site => "http://localhost:8000", + :authorize_path => "/oauth/authorize" + } + + uid do + raw_info["id"] + end + + info do + { + :email => raw_info["email"] + } + end + + def raw_info + @raw_info ||= access_token.get('/api/v1/me.json').parsed + end + end + end +end \ No newline at end of file diff --git a/lib/orbit_core_lib.rb b/lib/orbit_core_lib.rb index b44b32e..e0c5f6e 100644 --- a/lib/orbit_core_lib.rb +++ b/lib/orbit_core_lib.rb @@ -234,7 +234,7 @@ module OrbitCoreLib when :sub_manager @open ||= check_sub_manager when :approver - @open ||= check_sub_manager + @open ||= check_approver when :user @open ||= true when :visitor diff --git a/lib/store.rb b/lib/store.rb new file mode 100644 index 0000000..3d8e400 --- /dev/null +++ b/lib/store.rb @@ -0,0 +1,52 @@ +require 'httparty' +class Store + include HTTParty + + format :json + base_uri STORE_CONFIG[:store_settings]["api_url"] + + def initialize(site_id,site_token,api_key) + @options_for_get = { + headers: { + "Authorization" => api_key, + "X-SiteToken" => site_token, + "X-SiteId" => site_id, + "Content-Type" => "application/json", + 'Accept' => 'application/json' + } + } + + @options_for_client = { + headers: { + "Authorization" => api_key, + "Content-Type" => "application/json", + 'Accept' => 'application/json' + } + } + end + + def templates(options={}) + options = @options_for_get + self.class.get('/templates', options) + end + + def get_template(id) + options = @options_for_get + self.class.get("/templates/#{id}", options) + end + + def extensions(options={}) + options = @options_for_get + self.class.get('/extensions', options) + end + + def get_extension(id) + options = @options_for_get + self.class.get("/extensions/#{id}", options) + end + + def post_client(site_id,site_token,site_name) + options = @options_for_client.merge({ :body => {:site_name => site_name, :site_id => site_id, :site_token => site_token}.to_json }) + self.class.post('/clients', options ) + end +end \ No newline at end of file
"> - + diff --git a/app/views/admin/template_store/index.html.erb b/app/views/admin/template_store/index.html.erb index e4c8a61..fd462ec 100644 --- a/app/views/admin/template_store/index.html.erb +++ b/app/views/admin/template_store/index.html.erb @@ -5,5 +5,6 @@ Status