devise_invitable icon indicating copy to clipboard operation
devise_invitable copied to clipboard

Disallow invitations with existing email

Open teosz opened this issue 10 years ago • 7 comments

Hello, Is there a way to don't let invites for existing user happen?

teosz avatar Nov 28 '16 16:11 teosz

Can you explain more? Are you getting invitations for registered users or users who already accepted invitation? Or are you talking about disable resending invitations?

scambra avatar Dec 01 '16 10:12 scambra

I'm getting for invitations for users who already accepted the invitation. I'm not talking about resending invitations.

teosz avatar Dec 02 '16 13:12 teosz

It shouldn't happen, deviseinvitable looks for existing user with email and check if it has a pending invitation:

          elsif !invitable.invited_to_sign_up? || !self.resend_invitation
            invite_key_array.each do |key|
              invitable.errors.add(key, :taken)
            end
          end

Are you using some custom code?

If you look for user with that email, what invited_to_sign_up? returns?

scambra avatar Dec 02 '16 13:12 scambra

The user manually created by seed. invitable.invited_to_sign_up? returns true. This is how seeds looks like:

user = User.new(
    user: 'user',
    password: 'pass',
   ......
}
...
user.save!

I've tried to manually invite the user in seed with skip_invitation parameter but I think the problem is at:

      def accept_invitation!
        if self.invited_to_sign_up?
          @accepting_invitation = true
          run_callbacks :invitation_accepted do
            self.accept_invitation
            self.confirmed_at = self.invitation_accepted_at if self.respond_to?(:confirmed_at)
            self.save
          end
        end
      end

Maybe accept_invitation should happen when user is invited with skip_invitation param too because self.invitation_token is not nill.

teosz avatar Dec 02 '16 14:12 teosz

You must be creating user with invitation_token present, so invited_to_sign_up? is true, in that case is ok to allow sending invitation because user didn't accept invitation, and resend_invitations is enabled.

You didn't post whole seed for user, so just guessing

scambra avatar Dec 02 '16 15:12 scambra

I'm getting this issue as well using ~> 1.7

Expected I create a new user, invite the new user, and the new user accepts the invitation. When I run .invite! again the invitation process isn't run and it returns false.

Actual I create a new user, invite the new user, and the new user accepts the invitation. However, when I invite the user again the invitation process is run and returns #<Delayed::Backend::ActiveRecord::Job:0x00007fbf87ddf678 ...> object.

The steps involved to replicate:

new_user = User.new({account: account, subdomain: 'testdomain', email: '[email protected]'})
new_user.invite!
=> true
new_user.invited_to_sign_up?
=> true
new_user.update_attributes(password: 'aoeui123', password_confirmation: 'aoeui123')
=>
new_user.accept_invitation!
=> #<Delayed::Backend::ActiveRecord::Job:0x00007fbf87ddf678 ... >
new_user.invited_to_sign_up?
=> false
new_user.invite!
=> true

The one time I've gotten it to fail is when I run the User.new... step with the same details as another user and run the.invite! method. It fails because a user already exists.

codeinaire avatar Mar 05 '19 04:03 codeinaire

accept_invitation! should return true or false. Have you changed any method in your model?

Your test is not valid, you should use User.invite! instead of User.new and user.invite!, check to avoid inviting existing user is in User.invite!, if you call instance method instead of class method, you are skipping DeviseInvitable checks, and you must do whatever checks you want.

scambra avatar Mar 06 '19 09:03 scambra