public function attemptToJoin(Organization $organization) { /** * If the user is the admin of this organization. */ if ($this->hasRole($organization->roleAdminName())) { $this->organizations()->attach($this->id); return true; } /** * If the user's email is in one of the organization's allowed email domains. */ $user_email_domain = array_pop(explode('@', $this->email)); if ($organization->emailDomains()->where('domain', $user_email_domain)->exists()) { $this->organizations()->attach($this->id); return true; } // TODO: check if user is one of the invited return false; }