Skip to content

Commit b75526a

Browse files
authored
Merge pull request #1527 from ActiveLearningStudio/release/cherry-pick-msteams
Release/cherry pick msteams
2 parents 57c437a + 93383a0 commit b75526a

File tree

8 files changed

+136
-30
lines changed

8 files changed

+136
-30
lines changed

app/Http/Controllers/Api/V1/MicroSoftTeamController.php

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,12 @@ public function getAccessToken(Request $request)
131131
*/
132132
public function getAccessTokenViaCode(GetTokenViaCode $request)
133133
{
134-
$accessToken = $this->microsoftTeamRepository->getTokenViaCode($request);
135-
136-
if ($accessToken && array_key_exists('access_token', $accessToken)) {
137-
$request['token'] = $accessToken['access_token'];
138-
$getSubmission = $this->microsoftTeamRepository->getSubmission($request);
134+
$accessToken = $this->microsoftTeamRepository->getTokenViaCode($request);
139135

140-
if ($getSubmission && array_key_exists('status', $getSubmission)) {
136+
if ($accessToken && array_key_exists('access_token', $accessToken)) {
137+
$request['token'] = $accessToken['access_token'];
138+
$getSubmission = $this->microsoftTeamRepository->getSubmission($request);
139+
141140
return response([
142141
'status_code' => 200,
143142
'message' => 'Token fetched successfully.',
@@ -146,17 +145,11 @@ public function getAccessTokenViaCode(GetTokenViaCode $request)
146145
'refresh_token' => $accessToken['refresh_token']
147146
], 200);
148147
}
149-
150148
return response([
151149
'status_code' => 424,
152-
'errors' => $getSubmission['error'],
153-
], 500);
154-
}
155-
return response([
156-
'status_code' => 424,
157-
'errors' => $accessToken['error'],
158-
'message' => $accessToken['error_description']
159-
], 500);
150+
'errors' => $accessToken['error'],
151+
'message' => $accessToken['error_description']
152+
], 500);
160153
}
161154

162155
/**
@@ -268,16 +261,16 @@ public function getUserPofile(GetUserProfileRequest $request)
268261
if ($accessToken && array_key_exists('access_token', $accessToken)) {
269262
$getProfile = $this->microsoftTeamRepository->getUserProfile($accessToken['access_token']);
270263

271-
return response([
272-
'profile' => $getProfile,
273-
], 200);
274-
} else {
275-
return response([
276-
'status_code' => 424,
277-
'errors' => $accessToken['error'],
278-
'message' => $accessToken['error_description']
279-
], 500);
264+
if ($getProfile && array_key_exists('displayName', $getProfile)) {
265+
return response([
266+
'profile' => $getProfile,
267+
], 200);
268+
}
280269
}
270+
271+
return response([
272+
'profile' => 'Something went wrong with the login code or token, unable to fetch user profile',
273+
], 400);
281274
}
282275

283276
/**

app/Http/Requests/V1/GetTokenViaCode.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ public function rules()
2525
{
2626
return [
2727
'code' => 'required',
28-
'clientId' => 'required',
29-
'secretId' => 'required',
3028
'classId' => 'required',
3129
'assignmentId' => 'required',
3230
'submissionId' => 'required',
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Http\Requests\V1;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class GetUserProfileRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*
12+
* @return bool
13+
*/
14+
public function authorize()
15+
{
16+
return true;
17+
}
18+
19+
/**
20+
* Get the validation rules that apply to the request.
21+
*
22+
* @return array<string, mixed>
23+
*/
24+
public function rules()
25+
{
26+
return [
27+
'code' => 'required'
28+
];
29+
}
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Http\Requests\V1;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class SubmitAssignmentMst extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*
12+
* @return bool
13+
*/
14+
public function authorize()
15+
{
16+
return true;
17+
}
18+
19+
/**
20+
* Get the validation rules that apply to the request.
21+
*
22+
* @return array<string, mixed>
23+
*/
24+
public function rules()
25+
{
26+
return [
27+
'classId' => 'required',
28+
'assignmentId' => 'required',
29+
'submissionId' => 'required',
30+
'token' => 'required'
31+
];
32+
}
33+
}

app/Repositories/MicrosoftTeam/MicrosoftTeamRepository.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Repositories\BaseRepository;
88
use App\Repositories\User\UserRepositoryInterface;
99
use Illuminate\Support\Facades\Http;
10+
use stdClass;
1011

1112
class MicrosoftTeamRepository extends BaseRepository implements MicrosoftTeamRepositoryInterface
1213
{
@@ -91,8 +92,8 @@ public function getTokenViaCode($request)
9192

9293
$postInput = [
9394
'grant_type' => 'authorization_code',
94-
'client_id' => $request->clientId,
95-
'client_secret' => $request->secretId,
95+
'client_id' => $this->clientId,
96+
'client_secret' => $this->secretId,
9697
'code' => $request->code,
9798
'scope' => config('ms-team-configs.scope_for_token'),
9899

@@ -109,6 +110,27 @@ public function getTokenViaCode($request)
109110
return $responseBody;
110111
}
111112

113+
/**
114+
* @param $code string
115+
* @return string
116+
*/
117+
public function submitAssignment($data)
118+
{
119+
$apiURL = $this->landingUrl . 'education/classes/' . $data['classId'] . '/assignments/' . $data['assignmentId'] . '/submissions/' . $data['submissionId'] . '/submit';
120+
$headers = [
121+
'Content-length' => 0,
122+
'Content-type' => 'application/json',
123+
'Authorization' => 'Bearer ' . $data['token']
124+
];
125+
126+
$response = Http::withHeaders($headers)->post($apiURL);
127+
128+
$responseBody = json_decode($response->getBody(), true);
129+
$responseBody['statusCode'] = $response->status();
130+
131+
return $responseBody;
132+
}
133+
112134
/**
113135
* @param $token string
114136
* @param $data array
@@ -391,6 +413,23 @@ private function getUserDetails($token)
391413
return $responseBody['id'];
392414
}
393415

416+
/**
417+
* @param string $token
418+
*/
419+
public function getUserProfile($token)
420+
{
421+
$apiURL = $this->landingUrl . '/profile';
422+
$headers = [
423+
'Content-length' => 0,
424+
'Content-type' => 'application/json',
425+
'Authorization' => 'Bearer ' . $token
426+
];
427+
428+
$response = Http::withHeaders($headers)->get($apiURL);
429+
$responseBody = json_decode($response->getBody(), true);
430+
return $responseBody;
431+
}
432+
394433
/**
395434
* @param $token string
396435
* @param $classId string

app/Repositories/MicrosoftTeam/MicrosoftTeamRepositoryInterface.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ public function getTokenViaCode($code);
2626
*/
2727
public function getSubmission($request);
2828

29+
/**
30+
* @param $object
31+
* @return string
32+
*/
33+
public function submitAssignment($request);
34+
2935
/**
3036
* @param $gid int
3137
* @return string
@@ -40,10 +46,15 @@ public function getLoginUrl($gid);
4046
*/
4147
public function getClassesList($token);
4248

49+
/**
50+
* @param $token string
51+
* @return
52+
*/
53+
public function getUserProfile($token);
54+
4355
/**
4456
* @param $token string
4557
* @param $data array
46-
*
4758
* @return int
4859
*/
4960
public function createMsTeamClass($token, $data);

config/ms-team-configs.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
'redirect_url' => env('MSTEAMS_REDIRECT_URL'),
1010
'assignment_due_days' => env('ASSIGNMENT_DUE_DATE_DAYS'),
1111
'scope' => 'https://graph.microsoft.com/.default',
12-
'scope_for_token' => 'https://graph.microsoft.com/User.Read https://graph.microsoft.com/Mail.Read'
12+
'scope_for_token' => 'https://graph.microsoft.com/User.Read https://graph.microsoft.com/Mail.Read offline_access'
1313
];
1414

routes/api.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
Route::get('checkemail/{email}', 'Auth\AuthController@checkEmail');
3535
Route::get('microsoft-team/get-access-token', 'Api\V1\MicroSoftTeamController@getAccessToken');
3636
Route::get('microsoft-team/get-access-token-via-code', 'Api\V1\MicroSoftTeamController@getAccessTokenViaCode');
37+
Route::get('microsoft-team/get-user-profile', 'Api\V1\MicroSoftTeamController@getUserPofile');
38+
Route::post('microsoft-team/submit-assignment', 'Api\V1\MicroSoftTeamController@submitAssignment');
3739

3840
Route::group(['prefix' => 'v1', 'namespace' => 'Api\V1'], function () {
3941
Route::get('projects/{project}/load-shared', 'ProjectController@loadShared');

0 commit comments

Comments
 (0)