From cc2ab28d166d0d089235e85c24da31643798135f Mon Sep 17 00:00:00 2001 From: Andreas Stephanides Date: Mon, 8 Jun 2015 22:03:14 +0200 Subject: [PATCH] AutoCommit Mon Jun 8 22:03:14 CEST 2015 --- app/controllers/.#calentries_controller.rb | 1 + app/controllers/application_controller.rb | 2 +- app/controllers/calendars_controller.rb | 5 +++-- app/controllers/calentries_controller.rb | 2 +- app/models/ability.rb | 14 +++++++++---- app/models/key.rb | 11 ++++++++++ app/views/calendars/index.ics.erb | 24 ++++++++++++++++++++++ app/views/rubriken/show.html.erb | 2 +- db/migrate/20150608193256_create_keys.rb | 15 ++++++++++++++ spec/factories/keys.rb | 11 ++++++++++ spec/models/key_spec.rb | 5 +++++ 11 files changed, 83 insertions(+), 9 deletions(-) create mode 120000 app/controllers/.#calentries_controller.rb create mode 100644 app/models/key.rb create mode 100644 app/views/calendars/index.ics.erb create mode 100644 db/migrate/20150608193256_create_keys.rb create mode 100644 spec/factories/keys.rb create mode 100644 spec/models/key_spec.rb diff --git a/app/controllers/.#calentries_controller.rb b/app/controllers/.#calentries_controller.rb new file mode 120000 index 0000000..5ab1a21 --- /dev/null +++ b/app/controllers/.#calentries_controller.rb @@ -0,0 +1 @@ +andreas@andreas-ThinkPad-S430.13138:1433770009 \ No newline at end of file diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e36543b..eb8fb64 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -48,7 +48,7 @@ u=current_user end def current_ability - @current_ability ||= Ability.new(current_user, request) + @current_ability ||= Ability.new(current_user, request, params[:key]) end def default_url_options {locale: I18n.locale, theme: nil , ansicht: nil} diff --git a/app/controllers/calendars_controller.rb b/app/controllers/calendars_controller.rb index 96dbbc4..733d25e 100644 --- a/app/controllers/calendars_controller.rb +++ b/app/controllers/calendars_controller.rb @@ -3,12 +3,13 @@ class CalendarsController < ApplicationController # GET /calendars.json load_and_authorize_resource def index - @calendars = Calendar.all - @calentries = Calentry.all + @calendars = Calendar.accessible_by(current_ability) + @calentries = Calentry.accessible_by(current_ability) respond_to do |format| format.html # index.html.erb format.json { render json: @calendars } + format.ics end end diff --git a/app/controllers/calentries_controller.rb b/app/controllers/calentries_controller.rb index bb14973..4997153 100644 --- a/app/controllers/calentries_controller.rb +++ b/app/controllers/calentries_controller.rb @@ -7,7 +7,7 @@ class CalentriesController < ApplicationController respond_to do |format| format.html {redirect_to rubriken_path} - + format.ics end end diff --git a/app/models/ability.rb b/app/models/ability.rb index 5a33594..6a1f586 100755 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -1,9 +1,15 @@ # -*- coding: utf-8 -*- class Ability include CanCan::Ability - def initialize(user,request=nil) + def initialize(user,request=nil,key=nil) loggedin=!(user.nil?) - user ||= User.new # guest user (not logged in) + unless key.nil? + k=Key.find_by_uuid(key) + if !k.nil? && k.is_valid && k.typ == 0 + user=k.user + end + end + user ||= User.new # guest user (not logged in) #----------------------------------------------------- @@ -103,7 +109,7 @@ end can :index, Rubrik can [:show], Rubrik, :public=>true can [:list], Neuigkeit, :cache_is_published=>true, :rubrik=>{:public=>true} - can :show, Neuigkeit, :rubrik=>{:public=>true} + can :show, Neuigkeit, :cache_is_published=>true, :rubrik=>{:public=>true} if loggedin end @@ -163,7 +169,7 @@ end can [:showics], Calendar can [:show], Calentry - if( user.has_role?("fetuser") || user.has_role?("fetadmin")) + if( user.has_role?("fetuser") || user.has_role?("fetadmin")|| (!k.nil? && k.typ==1 && (k.user.has_role?("fetuser")||k.user.has_role?("fetadmin")))) can [:show,:index], Calendar can [:edit, :update,:new,:create,:verwalten], Calendar can [:edit, :update,:new,:create,:verwalten,:delete], Calentry diff --git a/app/models/key.rb b/app/models/key.rb new file mode 100644 index 0000000..04291c4 --- /dev/null +++ b/app/models/key.rb @@ -0,0 +1,11 @@ +class Key < ActiveRecord::Base + attr_accessible :expire, :is_valid, :type, :user_id + belongs_to :parent, :polymorphic => true + belongs_to :user + before_create :create_unique_identifier + def create_unique_identifier + begin + self.uuid = SecureRandom.hex(10) # or whatever you chose like UUID tools + end while self.class.exists?(:uuid => uuid) + end +end diff --git a/app/views/calendars/index.ics.erb b/app/views/calendars/index.ics.erb new file mode 100644 index 0000000..1e82c5d --- /dev/null +++ b/app/views/calendars/index.ics.erb @@ -0,0 +1,24 @@ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:http://www.example.com/calendarapplication/ +METHOD:PUBLISH +X-WR-TIMEZONE:Europe/Vienna +BEGIN:VTIMEZONE +TZID:Europe/Vienna +BEGIN:DAYLIGHT +TZOFFSETFROM:+0100 +DTSTART:19810329T020000 +RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU +TZNAME:MESZ +END:DAYLIGHT +BEGIN:STANDARD +TZOFFSETFROM:+0200 +DTSTART:19961027T030000 +RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU +TZNAME:MEZ +END:STANDARD +END:VTIMEZONE +<% @calentries.each do |entry| %> +<%= render entry %> +<% end %> +END:VCALENDAR \ No newline at end of file diff --git a/app/views/rubriken/show.html.erb b/app/views/rubriken/show.html.erb index b52f5e2..928bbb3 100755 --- a/app/views/rubriken/show.html.erb +++ b/app/views/rubriken/show.html.erb @@ -52,7 +52,7 @@ <%= render 'calendars/calentries', :object=>@calentries %> <% end %> <% end %> -<%= link_to "ics-format", calendar_path(@rubrik.calendar,:format=>:ics) %> +<%= link_to "ics-format", calendar_path(@rubrik.calendar,:format=>:ics,:key=>current_user.nil? ? nil : Key.where(typ: 1, user_id: current_user.id,is_valid: true).last.try(:uuid)) %> diff --git a/db/migrate/20150608193256_create_keys.rb b/db/migrate/20150608193256_create_keys.rb new file mode 100644 index 0000000..84b7d60 --- /dev/null +++ b/db/migrate/20150608193256_create_keys.rb @@ -0,0 +1,15 @@ +class CreateKeys < ActiveRecord::Migration + def change + create_table :keys do |t| + t.string :uuid + t.datetime :expire + t.string :parent_type + t.integer:parent_id + t.integer :typ + t.integer :user_id + t.boolean :is_valid + + t.timestamps + end + end +end diff --git a/spec/factories/keys.rb b/spec/factories/keys.rb new file mode 100644 index 0000000..f544a93 --- /dev/null +++ b/spec/factories/keys.rb @@ -0,0 +1,11 @@ +FactoryGirl.define do + factory :key do + uuid "MyString" +expire "2015-06-08 21:32:56" +parent "" +type 1 +user_id 1 +is_valid false + end + +end diff --git a/spec/models/key_spec.rb b/spec/models/key_spec.rb new file mode 100644 index 0000000..f94d28a --- /dev/null +++ b/spec/models/key_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Key, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end