-
Notifications
You must be signed in to change notification settings - Fork 88
Prevent employee from inviting team members and fixed invitation flow #522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
483981b
Fixed the issue in inviting a existing and new user
c24f7f0
Merge branch 'develop' into prevent-employee-user-invitation
17491db
Added policy to prevent employee from inviting team member and fixed …
47209ad
Removed the overridden devise_controller? method
692f468
Commented company user related validation
9947f13
Removed test cases for company users validation
f2e8d9e
Merge branch 'develop' into prevent-employee-user-invitation
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class Users::InvitationsPolicy < ApplicationPolicy | ||
| def create? | ||
| user_owner_role? || user_admin_role? | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "rails_helper" | ||
|
|
||
| RSpec.describe Users::InvitationsPolicy, type: :policy do | ||
| let(:company) { create(:company) } | ||
| let(:user) { create(:user, current_workspace_id: company.id) } | ||
|
|
||
| context "when user is an admin" do | ||
| before do | ||
| create(:company_user, company:, user:) | ||
| user.add_role :admin, company | ||
| end | ||
|
|
||
| permissions :create? do | ||
| it "is permitted to create invitation" do | ||
| expect(described_class).to permit(user) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| context "when user is an owner" do | ||
| before do | ||
| create(:company_user, company:, user:) | ||
| user.add_role :owner, company | ||
| end | ||
|
|
||
| permissions :create? do | ||
| it "is permitted to create invitation" do | ||
| expect(described_class).to permit(user) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| context "when user is an employee" do | ||
| before do | ||
| create(:company_user, company:, user:) | ||
| user.add_role :employee, company | ||
| end | ||
|
|
||
| permissions :create? do | ||
| it "is not permitted to create invitation" do | ||
| expect(described_class).not_to permit(user) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| context "when user is a book keeper" do | ||
| before do | ||
| create(:company_user, company:, user:) | ||
| user.add_role :book_keeper, company | ||
| end | ||
|
|
||
| permissions :create? do | ||
| it "is not permitted to create invitation" do | ||
| expect(described_class).not_to permit(user) | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,35 +6,79 @@ | |
| let(:company) { create(:company) } | ||
| let(:user) { create(:user, current_workspace_id: company.id) } | ||
|
|
||
| before do | ||
| create(:company_user, company:, user:) | ||
| user.add_role :admin, company | ||
| sign_in user | ||
| send_request( | ||
| :post, user_invitation_path, params: { | ||
| user: { | ||
| first_name: user.first_name, | ||
| last_name: user.last_name, | ||
| email: "[email protected]", | ||
| roles: "employee" | ||
| } | ||
| }) | ||
| end | ||
| context "when new user is invited" do | ||
| before do | ||
| create(:company_user, company:, user:) | ||
| user.add_role :admin, company | ||
| sign_in user | ||
| send_request( | ||
| :post, user_invitation_path, params: { | ||
| user: { | ||
| first_name: user.first_name, | ||
| last_name: user.last_name, | ||
| email: "[email protected]", | ||
| roles: "employee" | ||
| } | ||
| }) | ||
| @user = User.find_by(email: "[email protected]") | ||
| end | ||
|
|
||
| it "creates new user with invitation token" do | ||
| expect(User.count).to eq(2) | ||
| expect(User.last.invitation_token).not_to be_nil | ||
| end | ||
| it "creates new user with invitation token" do | ||
| expect(User.count).to eq(2) | ||
| expect(@user.invitation_token).not_to be_nil | ||
| end | ||
|
|
||
| it "assigns company to new user" do | ||
| expect(User.last.companies).to include(company) | ||
| end | ||
| it "assigns company to new user" do | ||
| expect(@user.companies).to include(company) | ||
| end | ||
|
|
||
| it "assigns role to new user" do | ||
| expect(User.last.has_role?(:employee, company)).to be_truthy | ||
| it "assigns role to new user" do | ||
| expect(@user.has_role?(:employee, company)).to be_truthy | ||
| end | ||
|
|
||
| it "successfully redirects to team_index_path" do | ||
| expect(response).to redirect_to(team_index_path) | ||
| end | ||
| end | ||
|
|
||
| it "successfully redirects to team_index_path" do | ||
| expect(response).to redirect_to(team_index_path) | ||
| context "when existing user is invited" do | ||
| before do | ||
| ActionMailer::Base.deliveries.clear | ||
| create(:company_user, company:, user:) | ||
| user.add_role :admin, company | ||
| sign_in user | ||
| @user2 = create(:user, current_workspace_id: company.id) | ||
| send_request( | ||
| :post, user_invitation_path, params: { | ||
| user: { | ||
| first_name: @user2.first_name, | ||
| last_name: @user2.last_name, | ||
| email: @user2.email, | ||
| roles: "employee" | ||
| } | ||
| }) | ||
| @user2.reload | ||
| end | ||
|
|
||
| it "existing user won't have invitation token" do | ||
| expect(@user2.invitation_token).to be_nil | ||
| end | ||
|
|
||
| it "assigns company to existing user" do | ||
| expect(@user2.companies).to include(company) | ||
| end | ||
|
|
||
| it "assigns role to existing user" do | ||
| expect(@user2.has_role?(:employee, company)).to be_truthy | ||
| end | ||
|
|
||
| it "sents mail to invited users" do | ||
| expect(ActionMailer::Base.deliveries.count).to eq(1) | ||
| expect(ActionMailer::Base.deliveries.last.to).to include(@user2.email) | ||
| end | ||
|
|
||
| it "successfully redirects to team_index_path" do | ||
| expect(response).to redirect_to(team_index_path) | ||
| end | ||
| end | ||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but for the first-time user they have to set the password right?. In that case I feel it should be the
Accept Invitation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, for non-existing users the
ifblock will be executed and the link in the mail will haveYes, it's me, confirm my email address, when the user clicks on that link it will take them to the set password page.For existing users the
elseblock will be executed and the link in the email will haveVisit workspace, when clicked it will take them to the root path.