Skip to content

Release/cherry pick msteams #1527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 17 additions & 24 deletions app/Http/Controllers/Api/V1/MicroSoftTeamController.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,12 @@ public function getAccessToken(Request $request)
*/
public function getAccessTokenViaCode(GetTokenViaCode $request)
{
$accessToken = $this->microsoftTeamRepository->getTokenViaCode($request);

if ($accessToken && array_key_exists('access_token', $accessToken)) {
$request['token'] = $accessToken['access_token'];
$getSubmission = $this->microsoftTeamRepository->getSubmission($request);
$accessToken = $this->microsoftTeamRepository->getTokenViaCode($request);

if ($getSubmission && array_key_exists('status', $getSubmission)) {
if ($accessToken && array_key_exists('access_token', $accessToken)) {
$request['token'] = $accessToken['access_token'];
$getSubmission = $this->microsoftTeamRepository->getSubmission($request);

return response([
'status_code' => 200,
'message' => 'Token fetched successfully.',
Expand All @@ -146,17 +145,11 @@ public function getAccessTokenViaCode(GetTokenViaCode $request)
'refresh_token' => $accessToken['refresh_token']
], 200);
}

return response([
'status_code' => 424,
'errors' => $getSubmission['error'],
], 500);
}
return response([
'status_code' => 424,
'errors' => $accessToken['error'],
'message' => $accessToken['error_description']
], 500);
'errors' => $accessToken['error'],
'message' => $accessToken['error_description']
], 500);
}

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

return response([
'profile' => $getProfile,
], 200);
} else {
return response([
'status_code' => 424,
'errors' => $accessToken['error'],
'message' => $accessToken['error_description']
], 500);
if ($getProfile && array_key_exists('displayName', $getProfile)) {
return response([
'profile' => $getProfile,
], 200);
}
}

return response([
'profile' => 'Something went wrong with the login code or token, unable to fetch user profile',
], 400);
}

/**
Expand Down
2 changes: 0 additions & 2 deletions app/Http/Requests/V1/GetTokenViaCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public function rules()
{
return [
'code' => 'required',
'clientId' => 'required',
'secretId' => 'required',
'classId' => 'required',
'assignmentId' => 'required',
'submissionId' => 'required',
Expand Down
30 changes: 30 additions & 0 deletions app/Http/Requests/V1/GetUserProfileRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Requests\V1;

use Illuminate\Foundation\Http\FormRequest;

class GetUserProfileRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
return [
'code' => 'required'
];
}
}
33 changes: 33 additions & 0 deletions app/Http/Requests/V1/SubmitAssignmentMst.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Http\Requests\V1;

use Illuminate\Foundation\Http\FormRequest;

class SubmitAssignmentMst extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
return [
'classId' => 'required',
'assignmentId' => 'required',
'submissionId' => 'required',
'token' => 'required'
];
}
}
43 changes: 41 additions & 2 deletions app/Repositories/MicrosoftTeam/MicrosoftTeamRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Repositories\BaseRepository;
use App\Repositories\User\UserRepositoryInterface;
use Illuminate\Support\Facades\Http;
use stdClass;

class MicrosoftTeamRepository extends BaseRepository implements MicrosoftTeamRepositoryInterface
{
Expand Down Expand Up @@ -91,8 +92,8 @@ public function getTokenViaCode($request)

$postInput = [
'grant_type' => 'authorization_code',
'client_id' => $request->clientId,
'client_secret' => $request->secretId,
'client_id' => $this->clientId,
'client_secret' => $this->secretId,
'code' => $request->code,
'scope' => config('ms-team-configs.scope_for_token'),

Expand All @@ -109,6 +110,27 @@ public function getTokenViaCode($request)
return $responseBody;
}

/**
* @param $code string
* @return string
*/
public function submitAssignment($data)
{
$apiURL = $this->landingUrl . 'education/classes/' . $data['classId'] . '/assignments/' . $data['assignmentId'] . '/submissions/' . $data['submissionId'] . '/submit';
$headers = [
'Content-length' => 0,
'Content-type' => 'application/json',
'Authorization' => 'Bearer ' . $data['token']
];

$response = Http::withHeaders($headers)->post($apiURL);

$responseBody = json_decode($response->getBody(), true);
$responseBody['statusCode'] = $response->status();

return $responseBody;
}

/**
* @param $token string
* @param $data array
Expand Down Expand Up @@ -391,6 +413,23 @@ private function getUserDetails($token)
return $responseBody['id'];
}

/**
* @param string $token
*/
public function getUserProfile($token)
{
$apiURL = $this->landingUrl . '/profile';
$headers = [
'Content-length' => 0,
'Content-type' => 'application/json',
'Authorization' => 'Bearer ' . $token
];

$response = Http::withHeaders($headers)->get($apiURL);
$responseBody = json_decode($response->getBody(), true);
return $responseBody;
}

/**
* @param $token string
* @param $classId string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public function getTokenViaCode($code);
*/
public function getSubmission($request);

/**
* @param $object
* @return string
*/
public function submitAssignment($request);

/**
* @param $gid int
* @return string
Expand All @@ -40,10 +46,15 @@ public function getLoginUrl($gid);
*/
public function getClassesList($token);

/**
* @param $token string
* @return
*/
public function getUserProfile($token);

/**
* @param $token string
* @param $data array
*
* @return int
*/
public function createMsTeamClass($token, $data);
Expand Down
2 changes: 1 addition & 1 deletion config/ms-team-configs.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
'redirect_url' => env('MSTEAMS_REDIRECT_URL'),
'assignment_due_days' => env('ASSIGNMENT_DUE_DATE_DAYS'),
'scope' => 'https://graph.microsoft.com/.default',
'scope_for_token' => 'https://graph.microsoft.com/User.Read https://graph.microsoft.com/Mail.Read'
'scope_for_token' => 'https://graph.microsoft.com/User.Read https://graph.microsoft.com/Mail.Read offline_access'
];

2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
Route::get('checkemail/{email}', 'Auth\AuthController@checkEmail');
Route::get('microsoft-team/get-access-token', 'Api\V1\MicroSoftTeamController@getAccessToken');
Route::get('microsoft-team/get-access-token-via-code', 'Api\V1\MicroSoftTeamController@getAccessTokenViaCode');
Route::get('microsoft-team/get-user-profile', 'Api\V1\MicroSoftTeamController@getUserPofile');
Route::post('microsoft-team/submit-assignment', 'Api\V1\MicroSoftTeamController@submitAssignment');

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