mpociot / teamwork

User to Team associations with invitation system for the Laravel 5 Framework
MIT License
1.07k stars 170 forks source link

attachTeam does not really checks for attachment existence (not unique members) #26

Closed JesusLeon closed 8 years ago

JesusLeon commented 8 years ago

Hi @mpociot,

I'm wondering if the test UserHasTeamsTraitTest@testAttachTeamDoesNotAttachTeamIdWhenItExists is actually testing the right thing: to prevent that a team is attached to a user more than once?

The reason is that while playing with the package, when attaching a team to a user twice on the same routine it does not prevent a double attachment.

Example:

// this will attach twice:
$user->attachTeam(1);
$user->attachTeam(1);

// this will also attach more than once:
$user->attachTeams([1, 2, 1]);

I guess you assumed that the team_ids are sanitized before using the attachTeam(s) method, but it would be good to have this as default on the package logics.

After playing a bit more, I noticed that eloquent many-to-many relation's attach() method does not reloads the relation after attaching, and therefore, using something like $user->teams->contains( $team_id ) will return always false.

By manually loading the relations before checking for existence, did the trick:

// File: src/Mpociot/Teamwork/Traits/UserHasTeams.php:147

$this->load('teams'); // Manually load the relations to have fresh data.

if( !$this->teams->contains( $team ) )
// ...

If this is correct, I would create a PR but unfortunately I'm not so familiar with the testing part. I will review it though and as soon I'm confident about it, I will send the PR (if I happen to have it ready faster than you :).

Thanks, J

mpociot commented 8 years ago

Hey,

sorry for the late reply and thank you for taking the time to create this issue!

It would be great if you could create a PR for this - don't worry too much about the testing part, as we can work this out together :)

sukelali commented 8 years ago

sir, Can you please give me an example project for your Teamwork package. I can't send invitation properly and also can't check invitation.please help me