| @@ -2,9 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace FluentCommunity\Modules\Auth\Classes; |
| 4 | 4 | |
| 5 | 5 | use FluentCommunity\App\Http\Controllers\Controller; |
| 6 | -use FluentCommunity\App\Models\BaseSpace; | |
| 6 | +use FluentCommunity\App\Models\Space; | |
| 7 | 7 | use FluentCommunity\App\Models\SpaceUserPivot; |
| 8 | 8 | use FluentCommunity\App\Services\Helper; |
| 9 | 9 | use FluentCommunity\App\Services\ProfileHelper; |
| 10 | 10 | use FluentCommunity\Framework\Http\Request\Request; |
| @@ -13,12 +13,15 @@ | ||
| 13 | 13 | class InvitationController extends Controller |
| 14 | 14 | { |
| 15 | 15 | public function getInvitations(Request $request) |
| 16 | 16 | { |
| 17 | - $user = $this->getUser(); | |
| 17 | + $user = $this->getUser(true); | |
| 18 | 18 | $isMod = $user->isCommunityModerator(); |
| 19 | 19 | $spaceId = intval($request->get('space_id')); |
| 20 | 20 | |
| 21 | + $space = Space::findOrFail($spaceId); | |
| 22 | + $space->verifyUserPermisson($user, 'community_moderator'); | |
| 23 | + | |
| 21 | 24 | $status = $request->get('status', 'pending'); |
| 22 | 25 | if ($status == 'all') { |
| 23 | 26 | $status = ''; |
| 24 | 27 | } |
| @@ -38,8 +41,14 @@ | ||
| 38 | 41 | }) |
| 39 | 42 | ->orderBy('id', 'desc') |
| 40 | 43 | ->paginate(); |
| 41 | 44 | |
| 45 | + foreach ($inviatations as $inviatation) { | |
| 46 | + if ($inviatation->isValid()) { | |
| 47 | + $inviatation->access_url = $inviatation->getAccessUrl(); | |
| 48 | + } | |
| 49 | + } | |
| 50 | + | |
| 42 | 51 | return [ |
| 43 | 52 | 'invitations' => $inviatations, |
| 44 | 53 | 'is_mod' => $isMod |
| 45 | 54 | ]; |
| @@ -47,10 +56,15 @@ | ||
| 47 | 56 | |
| 48 | 57 | public function delete(Request $request, $invitationId) |
| 49 | 58 | { |
| 50 | 59 | $invitationId = intval($invitationId); |
| 51 | - Invitation::findOrFail($invitationId)->delete(); | |
| 60 | + $inivitation = Invitation::findOrFail($invitationId); | |
| 61 | + $user = $this->getUser(true); | |
| 62 | + $space = Space::findOrFail($inivitation->post_id); | |
| 63 | + $space->verifyUserPermisson($user, 'community_moderator'); | |
| 52 | 64 | |
| 65 | + $inivitation->delete(); | |
| 66 | + | |
| 53 | 67 | return [ |
| 54 | 68 | 'message' => __('Invitation deleted successfully', 'fluent-community') |
| 55 | 69 | ]; |
| 56 | 70 | } |
| @@ -58,14 +72,18 @@ | ||
| 58 | 72 | public function store(Request $request) |
| 59 | 73 | { |
| 60 | 74 | $data = $request->all(); |
| 61 | 75 | $this->validate($data, [ |
| 62 | - 'email' => 'required|email', | |
| 76 | + 'email' => 'required|email', | |
| 77 | + 'space_id' => 'required' | |
| 63 | 78 | ]); |
| 64 | 79 | |
| 65 | 80 | $spaceId = (int)Arr::get($data, 'space_id'); |
| 66 | 81 | $user = $this->getUser(true); |
| 67 | 82 | |
| 83 | + $space = Space::findOrFail($spaceId); | |
| 84 | + $space->verifyUserPermisson($user, 'community_moderator'); | |
| 85 | + | |
| 68 | 86 | $inviteeEmail = sanitize_email(Arr::get($data, 'email')); |
| 69 | 87 | |
| 70 | 88 | $indivatationData = array_filter([ |
| 71 | 89 | 'email' => $inviteeEmail, |
| @@ -70,32 +88,57 @@ | ||
| 70 | 88 | $indivatationData = array_filter([ |
| 71 | 89 | 'email' => $inviteeEmail, |
| 72 | 90 | 'user_id' => $user->ID, |
| 73 | 91 | 'invitee_name' => sanitize_text_field(Arr::get($data, 'invitee_name')), |
| 92 | + 'space_id' => $space->id | |
| 74 | 93 | ]); |
| 75 | 94 | |
| 76 | - if ($spaceId) { | |
| 77 | - $space = BaseSpace::findOrFail($spaceId); | |
| 78 | - $spaceRole = $user->getSpaceRole($space); | |
| 95 | + $invitation = InvitationService::invite($indivatationData); | |
| 96 | + if (is_wp_error($invitation)) { | |
| 97 | + return $this->sendError([ | |
| 98 | + 'message' => $invitation->get_error_message() | |
| 99 | + ]); | |
| 100 | + } | |
| 79 | 101 | |
| 80 | - if (!$spaceRole || $spaceRole == 'pending') { | |
| 81 | - $this->sendError([ | |
| 82 | - 'message' => __('You are not permitted to invite', 'fluent-community') | |
| 83 | - ]); | |
| 84 | - } | |
| 102 | + // Now send the email for the invitation | |
| 103 | + InvitationService::sendInvitationEmail($invitation); | |
| 85 | 104 | |
| 86 | - $isMod = in_array($spaceRole, ['admin', 'moderator']); | |
| 105 | + return [ | |
| 106 | + 'message' => __('Invitation has been sent successfully', 'fluent-community'), | |
| 107 | + 'invitation' => $invitation | |
| 108 | + ]; | |
| 109 | + } | |
| 87 | 110 | |
| 88 | - if (!$isMod && $space->privacy != 'public') { | |
| 89 | - $this->sendError([ | |
| 90 | - 'message' => __('You are not permitted to invite', 'fluent-community') | |
| 91 | - ]); | |
| 92 | - } | |
| 111 | + public function createNewLink(Request $request) | |
| 112 | + { | |
| 113 | + $data = $request->all(); | |
| 114 | + $this->validate($data, [ | |
| 115 | + 'title' => 'required', | |
| 116 | + 'space_id' => 'required', | |
| 117 | + ]); | |
| 93 | 118 | |
| 94 | - $indivatationData['space_id'] = $space->id; | |
| 119 | + $spaceId = (int)Arr::get($data, 'space_id'); | |
| 120 | + $space = Space::findOrFail($spaceId); | |
| 121 | + $user = $this->getUser(true); | |
| 122 | + $space->verifyUserPermisson($user, 'community_moderator'); | |
| 123 | + | |
| 124 | + $indivatationData = array_filter([ | |
| 125 | + 'email' => '', | |
| 126 | + 'user_id' => $user->ID, | |
| 127 | + 'space_id' => $space->id, | |
| 128 | + 'title' => sanitize_text_field(Arr::get($data, 'title')), | |
| 129 | + 'limit' => sanitize_text_field(Arr::get($data, 'limit', 0)), | |
| 130 | + 'expire_date' => sanitize_text_field(Arr::get($data, 'expire_date', '')), | |
| 131 | + ]); | |
| 132 | + | |
| 133 | + $invitation = apply_filters('fluent_community/create_invitation_link', null, $indivatationData); | |
| 134 | + | |
| 135 | + if(!$invitation) { | |
| 136 | + return $this->sendError([ | |
| 137 | + 'message' => __('Something went wrong', 'fluent-community') | |
| 138 | + ]); | |
| 95 | 139 | } |
| 96 | 140 | |
| 97 | - $invitation = InvitationService::invite($indivatationData); | |
| 98 | 141 | if (is_wp_error($invitation)) { |
| 99 | 142 | return $this->sendError([ |
| 100 | 143 | 'message' => $invitation->get_error_message() |
| 101 | 144 | ]); |
| @@ -100,13 +143,12 @@ | ||
| 100 | 143 | 'message' => $invitation->get_error_message() |
| 101 | 144 | ]); |
| 102 | 145 | } |
| 103 | 146 | |
| 104 | - // Now send the email for the invitation | |
| 105 | - InvitationService::sendInvitationEmail($invitation); | |
| 147 | + $invitation->access_url = $invitation->getAccessUrl(); | |
| 106 | 148 | |
| 107 | 149 | return [ |
| 108 | - 'message' => __('Invitation has been sent successfully', 'fluent-community'), | |
| 150 | + 'message' => __('Invitation link has been created', 'fluent-community'), | |
| 109 | 151 | 'invitation' => $invitation |
| 110 | 152 | ]; |
| 111 | 153 | } |
| 112 | 154 | |
| @@ -112,12 +154,23 @@ | ||
| 112 | 154 | |
| 113 | 155 | public function resend(Request $request, $invitationId) |
| 114 | 156 | { |
| 115 | 157 | $invitation = Invitation::findOrFail($invitationId); |
| 158 | + /** @var Invitation $invitation */ | |
| 159 | + $user = $this->getUser(true); | |
| 160 | + $space = Space::findOrFail($invitation->post_id); | |
| 161 | + $space->verifyUserPermisson($user, 'community_moderator'); | |
| 116 | 162 | |
| 163 | + // Links have no recipient; resending would mail an empty address and burn a redemption. | |
| 164 | + if (!is_email($invitation->message)) { | |
| 165 | + return $this->sendError([ | |
| 166 | + 'message' => __('Only email invitations can be resent.', 'fluent-community') | |
| 167 | + ]); | |
| 168 | + } | |
| 169 | + | |
| 117 | 170 | if ($invitation->reactions_count > 5) { |
| 118 | 171 | return $this->sendError([ |
| 119 | - 'message' => __('You can not resend this invitation', 'fluent-community') | |
| 172 | + 'message' => __('You cannot resend this invitation', 'fluent-community') | |
| 120 | 173 | ]); |
| 121 | 174 | } |
| 122 | 175 | |
| 123 | 176 | InvitationService::sendInvitationEmail($invitation); |