forked from moodlehq/moodleapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request moodlehq#3754 from crazyserver/MOBILE-4323
MOBILE-4323 enrol: Create enrol delegates
- Loading branch information
Showing
42 changed files
with
1,819 additions
and
510 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// (C) Copyright 2015 Moodle Pty Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import { NgModule } from '@angular/core'; | ||
|
||
import { AddonEnrolFeeModule } from './fee/fee.module'; | ||
import { AddonEnrolGuestModule } from './guest/guest.module'; | ||
import { AddonEnrolPaypalModule } from './paypal/paypal.module'; | ||
import { AddonEnrolSelfModule } from './self/self.module'; | ||
|
||
@NgModule({ | ||
imports: [ | ||
AddonEnrolFeeModule, | ||
AddonEnrolGuestModule, | ||
AddonEnrolPaypalModule, | ||
AddonEnrolSelfModule, | ||
], | ||
}) | ||
export class AddonEnrolModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// (C) Copyright 2015 Moodle Pty Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import { APP_INITIALIZER, NgModule } from '@angular/core'; | ||
import { AddonEnrolFeeHandler } from './services/enrol-handler'; | ||
import { CoreEnrolDelegate } from '@features/enrol/services/enrol-delegate'; | ||
|
||
@NgModule({ | ||
providers: [ | ||
{ | ||
provide: APP_INITIALIZER, | ||
multi: true, | ||
useValue: () => { | ||
CoreEnrolDelegate.registerHandler(AddonEnrolFeeHandler.instance); | ||
}, | ||
}, | ||
], | ||
}) | ||
export class AddonEnrolFeeModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// (C) Copyright 2015 Moodle Pty Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import { Injectable } from '@angular/core'; | ||
import { CoreEnrolAction, CoreEnrolHandler } from '@features/enrol/services/enrol-delegate'; | ||
import { makeSingleton } from '@singletons'; | ||
|
||
/** | ||
* Enrol handler. | ||
*/ | ||
@Injectable({ providedIn: 'root' }) | ||
export class AddonEnrolFeeHandlerService implements CoreEnrolHandler { | ||
|
||
name = 'AddonEnrolFee'; | ||
type = 'fee'; | ||
enrolmentAction = CoreEnrolAction.BROWSER; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
async isEnabled(): Promise<boolean> { | ||
return true; | ||
} | ||
|
||
} | ||
|
||
export const AddonEnrolFeeHandler = makeSingleton(AddonEnrolFeeHandlerService); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// (C) Copyright 2015 Moodle Pty Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import { APP_INITIALIZER, NgModule } from '@angular/core'; | ||
import { AddonEnrolGuestHandler } from './services/enrol-handler'; | ||
import { CoreEnrolDelegate } from '@features/enrol/services/enrol-delegate'; | ||
|
||
@NgModule({ | ||
providers: [ | ||
{ | ||
provide: APP_INITIALIZER, | ||
multi: true, | ||
useValue: () => { | ||
CoreEnrolDelegate.registerHandler(AddonEnrolGuestHandler.instance); | ||
}, | ||
}, | ||
], | ||
}) | ||
export class AddonEnrolGuestModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"guestaccess_withpassword": "Guest access requires password", | ||
"guestaccess_withoutpassword": "Guest access", | ||
"passwordinvalid": "Incorrect access password, please try again" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// (C) Copyright 2015 Moodle Pty Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import { Injectable } from '@angular/core'; | ||
import { | ||
CoreEnrolAction, | ||
CoreEnrolCanAccessData, | ||
CoreEnrolGuestHandler, | ||
CoreEnrolInfoIcon, | ||
} from '@features/enrol/services/enrol-delegate'; | ||
import { makeSingleton } from '@singletons'; | ||
import { AddonEnrolGuest } from './guest'; | ||
import { CorePasswordModalResponse } from '@components/password-modal/password-modal'; | ||
import { CoreDomUtils } from '@services/utils/dom'; | ||
import { CoreWSError } from '@classes/errors/wserror'; | ||
import { CoreEnrol, CoreEnrolEnrolmentMethod } from '@features/enrol/services/enrol'; | ||
|
||
/** | ||
* Enrol handler. | ||
*/ | ||
@Injectable({ providedIn: 'root' }) | ||
export class AddonEnrolGuestHandlerService implements CoreEnrolGuestHandler { | ||
|
||
name = 'AddonEnrolGuest'; | ||
type = 'guest'; | ||
enrolmentAction = <CoreEnrolAction.GUEST> CoreEnrolAction.GUEST; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
async isEnabled(): Promise<boolean> { | ||
return true; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
async getInfoIcons(courseId: number): Promise<CoreEnrolInfoIcon[]> { | ||
const guestEnrolments = await CoreEnrol.getSupportedCourseEnrolmentMethods(courseId, { type: this.type }); | ||
|
||
for (const guestEnrolment of guestEnrolments) { | ||
const info = await AddonEnrolGuest.getGuestEnrolmentInfo(guestEnrolment.id); | ||
// Don't allow guest access if it requires a password if not supported. | ||
if (!info.passwordrequired) { | ||
return [{ | ||
label: 'addon.enrol_guest.guestaccess_withoutpassword', | ||
icon: 'fas-unlock', | ||
}]; | ||
} else { | ||
return [{ | ||
label: 'addon.enrol_guest.guestaccess_withpassword', | ||
icon: 'fas-key', | ||
}]; | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
async canAccess(method: CoreEnrolEnrolmentMethod): Promise<CoreEnrolCanAccessData> { | ||
const info = await AddonEnrolGuest.getGuestEnrolmentInfo(method.id); | ||
|
||
return { | ||
canAccess: info.status && (!info.passwordrequired || AddonEnrolGuest.isValidateGuestAccessPasswordAvailable()), | ||
requiresUserInput: info.passwordrequired, | ||
}; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
async validateAccess(method: CoreEnrolEnrolmentMethod): Promise<boolean> { | ||
const info = await AddonEnrolGuest.getGuestEnrolmentInfo(method.id); | ||
|
||
if (!info.status) { | ||
return false; | ||
} | ||
|
||
if (!info.passwordrequired) { | ||
return true; | ||
} | ||
|
||
if (!AddonEnrolGuest.isValidateGuestAccessPasswordAvailable()) { | ||
return false; | ||
} | ||
|
||
const validatePassword = async (password: string): Promise<CorePasswordModalResponse> => { | ||
const modal = await CoreDomUtils.showModalLoading('core.loading', true); | ||
|
||
try { | ||
const response = await AddonEnrolGuest.validateGuestAccessPassword(method.id, password); | ||
|
||
let error = response.hint; | ||
if (!response.validated && !error) { | ||
error = 'addon.enrol_guest.passwordinvalid'; | ||
} | ||
|
||
return { | ||
password, validated: response.validated, error, | ||
}; | ||
} finally { | ||
modal.dismiss(); | ||
} | ||
}; | ||
|
||
try { | ||
const response = await CoreDomUtils.promptPassword<CorePasswordModalResponse>({ | ||
title: method.name, | ||
validator: validatePassword, | ||
}); | ||
|
||
if (!response.validated) { | ||
return false; | ||
} | ||
} catch (error) { | ||
if (error instanceof CoreWSError) { | ||
throw error; | ||
} | ||
|
||
// Cancelled, return | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
async invalidate(method: CoreEnrolEnrolmentMethod): Promise<void> { | ||
return AddonEnrolGuest.invalidateGuestEnrolmentInfo(method.id); | ||
} | ||
|
||
} | ||
|
||
export const AddonEnrolGuestHandler = makeSingleton(AddonEnrolGuestHandlerService); |
Oops, something went wrong.