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

时间:2021-04-01 15:47:41

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_and_belongs_to_many用户,反之亦然)。 devise_invitable插件允许我的应用程序上的现有用户通过电子邮件向另一个诗歌用户发送邀请以在网站上注册,但我想要一些额外的功能:我希望用户能够邀请某人加入特定的组,以便尽快当他们在电子邮件中接受该邀请时,他们可以看到加入该组的链接。

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.

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

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

我应该覆盖邀请控制器方法,还是有另一个插件更好地服务于目的?

2 个解决方案

#1


7  

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:

我刚刚实现了一些非常相似的。我曾试图使用devise_invitable,但这需要很多工作。所以我找了一些其他的插件,但我找不到任何东西,我总结说编写我自己的实现更容易。这就是我做的:

  • 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

    一封电子邮件发送给受邀用户,它包含如下链接:/ 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

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

Hope this help

希望这有帮助

#2


6  

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.

我通过覆盖invites控制器完成了这个,执行此操作的指令位于github上的devise_invitable主页面中,所以我不会在此处进行讨论。

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).

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

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

在devise_invitable在用户表中创建新表后,新表将由用户标识索引。

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.

剩下的唯一问题是如何在电子邮件中添加其他参数,例如组名,我还没有解决。

#1


7  

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:

我刚刚实现了一些非常相似的。我曾试图使用devise_invitable,但这需要很多工作。所以我找了一些其他的插件,但我找不到任何东西,我总结说编写我自己的实现更容易。这就是我做的:

  • 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

    一封电子邮件发送给受邀用户,它包含如下链接:/ 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

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

Hope this help

希望这有帮助

#2


6  

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.

我通过覆盖invites控制器完成了这个,执行此操作的指令位于github上的devise_invitable主页面中,所以我不会在此处进行讨论。

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).

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

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

在devise_invitable在用户表中创建新表后,新表将由用户标识索引。

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.

剩下的唯一问题是如何在电子邮件中添加其他参数,例如组名,我还没有解决。