使用 devise_invitable 将用户添加到 Ruby on Rails 中的组?

发布于 2024-10-01 11:49:08 字数 417 浏览 0 评论 0原文

我的应用程序中有一个用户可以加入的“组”资源(组 has_and_belongs_to_many Users ,反之亦然)。 devise_invitable 插件允许我的应用上的现有用户通过电子邮件向其他潜在用户发送邀请以在网站上注册,但我想要一些额外的功能:我希望用户能够邀请某人加入特定群组,这样一旦他们接受电子邮件中的邀请,他们就可以看到加入链接那个集团。

有人用 devise_invitable 来做这样的事情吗?添加此额外功能的最佳方法是什么?看来我需要在邀请电子邮件中包含某种识别组的“组令牌”,然后将其传回用户单击接受邀请的 URL 中。

我应该覆盖邀请控制器方法,还是有另一个插件可以更好地达到目的?

I have a "Groups" resource within my app that Users can join (a Group has_and_belongs_to_many Users and vice versa). The devise_invitable plugin allows an existing User on my app to email an invitation to another poetntial User to register on the site, but I want some extra functionality: I want a User to be able to invite someone to a particular Group, so that as soon as they accept that invitation in the email, they can see a link to join that Group.

Has anyone used devise_invitable to do something like this? What's the best way to go about adding this extra functionality? It seems I'd need to include some sort of of "Group token" identifying the Group in the invite email, then pass it back in the URL that the user clicks to accept the invite.

Should I be overriding the invitations controller methods, or is there another plugin that serves the purpose better?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一生独一 2024-10-08 11:49:08

我刚刚实现了一些非常相似的。我尝试过使用 devise_invitable 但工作量很大。所以我寻找了一些其他插件,但我找不到任何东西,我得出的结论是编写自己的实现更容易。
这就是我所做的:

  • 添加邀请资源,

  • 当用户创建邀请时,我将我需要的所有信息存储在邀请字段中(它是序列化哈希)应用于将接受邀请的用户

  • 一封电子邮件被发送给受邀用户,它包含一个链接,如:/users/signup?invitation_token=1231223123

  • 当用户单击链接时,我创建用户并应用邀请中存储的所有信息,如群组成员身份、姓名、角色...

  • 如果受邀用户不需要注册该服务,则电子邮件中的链接会有所不同:invitations/123/accept?invitation_token= 12312312asdas324

希望有所帮助

I have just implemented some very similar. I have tried to use devise_invitable but it was to much work. So I looked for some other plugins but I could not find anything and I concluded it was just easier to write my own implementation.
This is what I did:

  • adding a invitation resource,

  • when a user create an invitation I store in an invitation field (it is a serialized hash) all the info I need to apply to the user that will accept the invitation

  • an email is sent out to the invited user, it contains a link like: /users/signup?invitation_token=1231223123

  • when user clicks on the link I create the user and applies all the info stored in the invitation like group membership, name, roles...

  • if the invited user does not need to signup for the service than the link in the email is different: invitations/123/accept?invitation_token=12312312asdas324

Hope this help

影子是时光的心 2024-10-08 11:49:08

我通过覆盖邀请控制器来完成此操作,执行此操作的说明位于 github 上的 devise_invitable 主页中,因此我不会在这里详细介绍。

为了安全起见,我创建了一个新表来保存额外的邀请信息,以便电子邮件接受链接不包含群组链接(我的群组仅通过私人邀请进行,因此我不希望受邀请的用户更改电子邮件中的邀请网址并可能添加其他未受邀请的群组)。

devise_invitable 在用户表中创建新表后,将通过用户 id 对其进行索引。

class Users::InvitesController < Devise::InvitationsController
.
.
def create

    # make sure the current user is connected to this object for security
    if @object.has_connection_with(current_user)

      # perform the invite    
      self.resource = resource_class.invite!(params[resource_name], current_inviter)

      #since we invite based on object, we must reference this object once the user accepts, so we store this in our Invites table
      object_invite = Invite.new(:invitable_type => @object.class.name, :invitable_id => @object.id, :user_id => self.resource.id, :inviter_id => current_inviter.profile.id)
      object_invite.save!

      if resource.errors.empty?
        set_flash_message :notice, :send_instructions, :email => self.resource.email
        #respond_with resource, :location => after_invite_path_for(resource)
        render :json => {:status => 'success'} and return
      else
        respond_with_navigational(resource) { render_with_scope :new }
      end
    end
  end

然后在您的更新方法中(同样在同一控制器中),您可以 find() 邀请记录并提取所需的信息,以便您将新用户连接到组。

剩下的唯一问题是如何向电子邮件添加其他参数,例如组名称,我尚未解决。

I've done this by overriding the invites controller, the instructions to do this are in the devise_invitable main page on github so I won't go into it here.

For security, I created a new table that holds extra invitation information so that the email accept link does not hold the group link (my groups are by private invites only so I don't want a invited user to alter the invite url in the email and potentially add other groups which they were not invited).

The new table is indexed by the user id after devise_invitable creates it in the user table.

class Users::InvitesController < Devise::InvitationsController
.
.
def create

    # make sure the current user is connected to this object for security
    if @object.has_connection_with(current_user)

      # perform the invite    
      self.resource = resource_class.invite!(params[resource_name], current_inviter)

      #since we invite based on object, we must reference this object once the user accepts, so we store this in our Invites table
      object_invite = Invite.new(:invitable_type => @object.class.name, :invitable_id => @object.id, :user_id => self.resource.id, :inviter_id => current_inviter.profile.id)
      object_invite.save!

      if resource.errors.empty?
        set_flash_message :notice, :send_instructions, :email => self.resource.email
        #respond_with resource, :location => after_invite_path_for(resource)
        render :json => {:status => 'success'} and return
      else
        respond_with_navigational(resource) { render_with_scope :new }
      end
    end
  end

Then in your update method (again in the same controller) you can find() the Invites record and pull the required info that will allow you to connect the new user to the group.

The only issue left is how to add additional parameters to the email, like group name, which I have yet to resolve.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文