mpociot / teamwork

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

Error in inviteToTeam function without an authenticated user #17

Closed kielabokkie closed 8 years ago

kielabokkie commented 8 years ago

Hi,

I'm using Teamwork for a REST API project and I'm having an issue with the inviteToTeam function. My API uses OAuth to authentication users so I don't have an "authenticated user" which causes an error on line 71 of Teamwork.php. Basically it tries to get the id of the authenticated user but $this->app->auth->user() will return null in my case, so get getKey() will throw a FatalErrorException.

I can write my own implementation of the inviteToTeam function but I wonder if there is a way around it or if you might support this use case in the future?

mpociot commented 8 years ago

Have you found a way around this issue?

For version 2 of this package it would be best if the user_id field is nullable and inviteToTeam inserts NULL instead of throwing a FatalErrorException.

kielabokkie commented 8 years ago

I got around it by creating my own version of the inviteToTeam function on my Team.php class (which extends TeamworkTeam):

class Team extends TeamworkTeam
{
    /**
     * Invite a user to the team.
     *
     * @param  string $email
     * @param  integer $inviterId
     * @param  callable $success
     */
    public function inviteUser($email, $inviterId, callable $success = null)
    {
        $invite = new TeamInvite;
        $invite->user_id = $inviterId;
        $invite->team_id = $this->id;
        $invite->type = 'invite';
        $invite->email = $email;
        $invite->accept_token = md5(uniqid(microtime()));
        $invite->deny_token = md5(uniqid(microtime()));
        $invite->save();

        if (is_null($success) === false) {
            $success($invite);
        }
    }
}

This allows me to pass the id of the inviter. Making user_id nullable is also a possibility to get around the issue but I think it's a good idea to allow the inviter id to be passed in manually so you don't rely on the authenticated user.

zakiaziz commented 8 years ago

I'm using Sentinel for auth and I also had this issue.

Thanks @kielabokkie I've implemented a similar solution