Skip to content

Commit

Permalink
v1.1 (#6)
Browse files Browse the repository at this point in the history
* Add completion condition testing and event monitoring to the playground

* Update all modules and fix service creation  bug

* Reference modules on github only

* Clean Vue components and add ability to control user/group/role combination

* Update tests and increase coverage

* Update docs

* Update changelog
  • Loading branch information
tobytwigger committed Feb 25, 2020
1 parent 6143241 commit 276bb14
Show file tree
Hide file tree
Showing 234 changed files with 193,725 additions and 1,175 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.1] - (25/02/2020)

### Added
- Completion condition testing
- See all fired events within a module

### Changed
- Authentication now returns a group and role when needed by the activity
- Creating a module instance accepts an 'activity_for' type, user, group or role

## [1.0.3] - (12/02/2020)

### Changed
Expand All @@ -31,7 +41,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Open and save module instances
- Change settings, permissions and third party connections

[Unreleased]: https://github.com/bristol-su/playground/compare/v1.0.3...HEAD
[Unreleased]: https://github.com/bristol-su/playground/compare/v1.1...HEAD
[1.1]: https://github.com/bristol-su/playground/compare/v1.0.3...v1.1
[1.0.3]: https://github.com/bristol-su/playground/compare/v1.0.2...v1.0.3
[1.0.2]: https://github.com/bristol-su/playground/compare/v1.0.1...v1.0.2
[1.0.1]: https://github.com/bristol-su/playground/compare/v1.0...v1.0.1
Expand Down
17 changes: 17 additions & 0 deletions app/Http/Controllers/Api/ModuleEventController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace BristolSU\Playground\Http\Controllers\Api;

use BristolSU\Playground\Http\Controllers\Controller;
use BristolSU\Support\Events\Contracts\EventRepository;
use BristolSU\Support\Module\Module;

class ModuleEventController extends Controller
{

public function index(Module $module, EventRepository $eventRepository)
{
return $eventRepository->allForModule($module->getAlias());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace BristolSU\Playground\Http\Controllers\Api;

use BristolSU\Playground\Http\Controllers\Controller;
use BristolSU\Support\ActivityInstance\Contracts\ActivityInstanceResolver;
use BristolSU\Support\Completion\Contracts\CompletionConditionInstanceRepository;
use BristolSU\Support\Completion\Contracts\CompletionConditionTester;
use BristolSU\Support\ModuleInstance\ModuleInstance;
use Illuminate\Http\Request;

/**
* Handle completion conditions
*/
class ModuleInstanceCompletionConditionController extends Controller
{

public function store(ModuleInstance $moduleInstance, Request $request, CompletionConditionInstanceRepository $conditionInstanceRepository)
{
$completion = $conditionInstanceRepository->create([
'alias' => $request->input('alias'),
'name' => 'CC For ' . $moduleInstance->name,
'description' => 'Completion Condition in the Playground',
'settings' => $request->input('settings')
]);
$moduleInstance->completion_condition_instance_id = $completion->id;
$moduleInstance->save();
return $completion;
}

public function test(ModuleInstance $moduleInstance, CompletionConditionTester $tester, ActivityInstanceResolver $activityInstanceResolver)
{
if($moduleInstance->completion_condition_instance_id === null) {
return abort(404, 'Could not find the completion condition');
}
$activityInstance = $activityInstanceResolver->getActivityInstance();
return [
'result' => $tester->evaluate($activityInstance, $moduleInstance->completionConditionInstance),
'percentage' => $tester->evaluatePercentage($activityInstance, $moduleInstance->completionConditionInstance)
];
}

}
17 changes: 17 additions & 0 deletions app/Http/Controllers/Api/ModuleInstanceEventController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace BristolSU\Playground\Http\Controllers\Api;

use BristolSU\Playground\Http\Controllers\Controller;
use BristolSU\Playground\Support\Events\Event;
use BristolSU\Support\ModuleInstance\ModuleInstance;

class ModuleInstanceEventController extends Controller
{

public function index(ModuleInstance $moduleInstance)
{
return Event::where('module_instance_id', $moduleInstance->id())->get();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ModuleModuleInstanceController extends Controller
public function store(Request $request, Module $module)
{
return (new ModuleInstanceFactory())->createModuleInstance(
$module, $request->input('name')
$module, $request->input('name'), $request->input('activity_for', 'user')
)->load('activity');
}

Expand Down
5 changes: 5 additions & 0 deletions app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
namespace BristolSU\Playground\Http\Controllers\Auth;

use BristolSU\Playground\Http\Controllers\Controller;
use BristolSU\Support\User\Contracts\UserAuthentication;
use BristolSU\Support\User\Contracts\UserRepository;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

class LoginController extends Controller
{
Expand Down Expand Up @@ -36,4 +40,5 @@ public function __construct()
{
$this->middleware('guest')->except('logout');
}

}
1 change: 0 additions & 1 deletion app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace BristolSU\Playground\Http\Controllers\Auth;

use BristolSU\Playground\Http\Controllers\Controller;
use BristolSU\Playground\User;
use BristolSU\ControlDB\Contracts\Repositories\DataUser;
use BristolSU\Support\User\Contracts\UserRepository;
use Illuminate\Foundation\Auth\RegistersUsers;
Expand Down
15 changes: 15 additions & 0 deletions app/Http/Controllers/Auth/ResetPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,19 @@ class ResetPasswordController extends Controller
* @var string
*/
protected $redirectTo = '/';

/**
* Get the password reset validation rules.
*
* @return array
*/
protected function rules()
{
return [
'token' => 'required',
'email' => 'required|email',
'password' => 'required|confirmed|min:6',
];
}

}
18 changes: 15 additions & 3 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace BristolSU\Playground\Providers;

use BristolSU\Playground\Support\Events\SaveEventInDatabase;
use BristolSU\Support\Events\Contracts\EventRepository;
use BristolSU\Support\Module\Contracts\ModuleRepository;
use Illuminate\Auth\Events\Registered;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

Expand All @@ -16,8 +19,17 @@ class EventServiceProvider extends ServiceProvider
* @var array
*/
protected $listen = [
// Registered::class => [
// SendEmailVerificationNotification::class,
// ],

];

public function listens()
{
$listens = [];
foreach(app(ModuleRepository::class)->all() as $module) {
foreach(app(EventRepository::class)->allForModule($module->getAlias()) as $event) {
$listens[$event['event']] = [SaveEventInDatabase::class];
}
}
return array_merge($this->listen, $listens);
}
}
3 changes: 3 additions & 0 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace BristolSU\Playground\Providers;

use BristolSU\Playground\Support\Permissions\ModulePermission;
use BristolSU\Support\Completion\Contracts\CompletionConditionInstance;
use BristolSU\Support\Completion\Contracts\CompletionConditionInstanceRepository;
use BristolSU\Support\Completion\Contracts\CompletionConditionRepository;
use BristolSU\Support\ModuleInstance\ModuleInstance;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
Expand Down
87 changes: 84 additions & 3 deletions app/Support/Authentication/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

namespace BristolSU\Playground\Support\Authentication;

use BristolSU\ControlDB\Contracts\Repositories\DataGroup;
use BristolSU\ControlDB\Contracts\Repositories\DataPosition;
use BristolSU\ControlDB\Contracts\Repositories\DataRole;
use BristolSU\ControlDB\Contracts\Repositories\Pivots\UserGroup;
use BristolSU\ControlDB\Contracts\Repositories\Pivots\UserRole;
use BristolSU\Support\Activity\Activity;
use BristolSU\Support\Authentication\Contracts\Authentication as AuthenticationContract;
use BristolSU\Support\User\Contracts\UserAuthentication;
use BristolSU\ControlDB\Contracts\Models\Group;
use BristolSU\ControlDB\Contracts\Models\Role;
use BristolSU\ControlDB\Contracts\Models\User;
use BristolSU\ControlDB\Contracts\Repositories\User as UserRepository;
use Illuminate\Contracts\Auth\Factory as AuthFactory;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Str;

/**
* Authentication contract that uses the database user
Expand Down Expand Up @@ -72,10 +79,17 @@ public function setUser(User $user)
*
* This function does not provide a way to get groups
*
* @return null
* @return Group|null
*/
public function getGroup()
{
if($this->activity()->exists) {
if($this->activity()->activity_for === 'group') {
return $this->getGroupForUser();
} elseif($this->activity()->activity_for === 'role') {
return $this->getRole()->group();
}
}
return null;
}

Expand All @@ -84,10 +98,13 @@ public function getGroup()
*
* This function does not provide a way to get roles
*
* @return null
* @return Role|null
*/
public function getRole()
{
if($this->activity()->exists && $this->activity()->activity_for === 'role') {
return $this->getRoleForUser();
}
return null;
}

Expand Down Expand Up @@ -127,4 +144,68 @@ public function setRole(Role $role)
public function reset(): void
{
}

protected function activity(): Activity
{
if(Request::route() && Request::route()->hasParameter('activity_slug')) {
return Request::route()->parameter('activity_slug');
}
return app(Activity::class);
}

/**
* Find or create a group the user is a member of
*
* @return Group
*/
protected function getGroupForUser(): Group {
$userGroupRepository = app(UserGroup::class);
$dataGroupRepository = app(DataGroup::class);
$groupRepository = app(\BristolSU\ControlDB\Contracts\Repositories\Group::class);

$groups = $userGroupRepository->getGroupsThroughUser($this->getUser());
if($groups->count() > 0) {
return $groups->first();
}

$dataGroup = $dataGroupRepository->create('Group ' . Str::random(), ($this->getUser()->data()->email() ?? '[email protected]'));
$group = $groupRepository->create($dataGroup->id());

$userGroupRepository->addUserToGroup($this->getUser(), $group);
return $group;
}

/**
* Find or create a role the user is in
*
* @return Role
*/
protected function getRoleForUser(): Role {
$userRoleRepository = app(UserRole::class);
$dataRoleRepository = app(DataRole::class);
$roleRepository = app(\BristolSU\ControlDB\Contracts\Repositories\Role::class);
$dataGroupRepository = app(DataGroup::class);
$groupRepository = app(\BristolSU\ControlDB\Contracts\Repositories\Group::class);
$dataPositionRepository = app(DataPosition::class);
$positionRepository = app(\BristolSU\ControlDB\Contracts\Repositories\Position::class);

$roles = $userRoleRepository->getRolesThroughUser($this->getUser());
if($roles->count() > 0) {
return $roles->first();
}

$dataRole = $dataRoleRepository->create('Role ' . Str::random(), ($this->getUser()->data()->email() ?? '[email protected]'));

$dataGroup = $dataGroupRepository->create('Group ' . Str::random(), ($this->getUser()->data()->email() ?? '[email protected]'));
$group = $groupRepository->create($dataGroup->id());

$dataPosition = $dataPositionRepository->create('Position ' . Str::random(), 'Position ' . Str::random());
$position = $positionRepository->create($dataPosition->id());

$role = $roleRepository->create($position->id(), $group->id(), $dataRole->id());

$userRoleRepository->addUserToRole($this->getUser(), $role);

return $role;
}
}
17 changes: 8 additions & 9 deletions app/Support/Authentication/UserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,31 @@ public function __construct(UserRepository $userRepository)
}

/**
* Retrieve a user by their unique email.
* Retrieve a user by their unique id.
*
* @param mixed $email
* @param mixed $id
* @return User|null
*/
public function retrieveById($email)
public function retrieveById($id)
{
try {
return $this->userRepository->getById($email);
return $this->userRepository->getById($id);
} catch (ModelNotFoundException $e) {}
return null;
}

/**
* Retrieve a user by their unique email and "remember me" token.
* Retrieve a user by their unique id and "remember me" token.
*
* @param mixed $email
* @param mixed $id
* @param string $token
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByToken($email, $token)
public function retrieveByToken($id, $token)
{
try {
$user = $this->userRepository->getFromRememberToken($token);
if($user->id === $email) {
if($user->id === $id) {
return $user;
}
} catch (ModelNotFoundException $e) {}
Expand All @@ -74,7 +74,6 @@ public function updateRememberToken(Authenticatable $user, $token)
*/
public function retrieveByCredentials(array $credentials)
{

try {
$dataUser = app(DataUser::class)->getWhere([
'email' => $credentials['email']
Expand Down
Loading

0 comments on commit 276bb14

Please sign in to comment.