【问题标题】:Rails webcal subcription url with user authentication token;带有用户身份验证令牌的 Rails webcal 订阅 url;
【发布时间】:2011-12-19 16:38:18
【问题描述】:

我正在尝试为我的应用用户创建一个链接,以用于订阅他们的帐户日历。我有一个在 rails calendar_publisher_lists_url(:format => :ics, :only_path => false,:protocol => "web cal") 中生成的有效链接 它会生成一个类似 webcal://mywebsite.com/calendar.ics 的链接 但是,这仅在用户登录时才有效。我需要在自动更新日历时链接工作,即使用户没有登录。因此我需要生成一个看起来像这样的链接: webcal://123@mywebsite.com/calendar.ics 其中“123”是用户的身份验证令牌。 如何生成此网址?

【问题讨论】:

    标签: ruby-on-rails icalendar


    【解决方案1】:

    如果安全不是问题,您可以更改路由以添加 id。

    match '/calendar/:id/calendar_feed', to: 'calendar#calendar_feed', :as => 'calendar_feed_path'
    

    然后在你的控制器中放

    def calendar_feed
      @user=User.find(params[:id])
      respond_to do |format|
        format.ics
      end
    end
    

    然后添加一个视图模板,根据您的逻辑进行调整以适应您的需求(此代码来自我的一个项目):

    BEGIN:VCALENDAR
    METHOD:PUBLISH
    VERSION:2.0
    PRODID:-//UserCalendar//EN
    X-WR-CALNAME:Name Of Website
    CALSCALE:GERGORIAN
    <% @users.each do |user| %>  #change logic to fit your needs
        <% user.day_offs.each do |day_off| %>
    BEGIN:VEVENT
    DTSTAMP:<%=Time.now.strftime("%Y%m%dT%H%M%SZ")%>
    UID:<%=day_off.id%>
    SUMMARY:<%= day_off.user.name.titleize %> | <%= day_off.do_type %>
    DTSTART:<%= day_off.start_date.strftime("%Y%m%d") %>
    <% end_day=day_off.end_date + 1.day %>
    DTEND:<%= end_day.strftime("%Y%m%d") %>
    END:VEVENT
    <% end %>
    <% end %>
    END:VCALENDAR
    

    那么提要链接将是

    <%= link_to("Subscribe to Calendar", "webcal://www.nameofwebsite.com/calendar_feed/#{current_user.user_id}/calendar_feed.ics" ) %>
    

    【讨论】:

      【解决方案2】:

      把它作为属性放到 URL 有什么问题?

      calendar_publisher_lists_url(:format => :ics, :only_path => false,:protocol => "webcal", :authentication_token => current_user.authentication_token)
      
      webcal://mywebsite.com/calendar.ics?authentication_token=123
      

      我的意思是除了安全问题...

      【讨论】:

      • 这太好了.. 谢谢!
      猜你喜欢
      • 1970-01-01
      • 2019-11-24
      • 2021-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      相关资源
      最近更新 更多