/** * Test LRS */ public function testLRS() { $lrs = new Lrs(); // Test title required. $values = array('title' => '', 'description' => \app\locker\helpers\Helpers::getRandomValue(), 'api' => array('basic_key' => \app\locker\helpers\Helpers::getRandomValue(), 'basic_secret' => \app\locker\helpers\Helpers::getRandomValue())); $validator = $lrs->validate($values); $this->assertTrue($validator->fails()); $this->assertFalse($validator->passes()); $values['title'] = \app\locker\helpers\Helpers::getRandomValue(); $validator = $lrs->validate($values); $this->assertTrue($validator->passes()); // Validate auth_service $values['auth_service_url'] = 'http://' . \app\locker\helpers\Helpers::getRandomValue() . '.adurolms.com'; $validator = $lrs->validate($values); $this->assertTrue($validator->passes()); // Add new lrs $lrs->title = $values['title']; $lrs->description = $values['description']; $lrs->api = $values['api']; $result = $lrs->save(); $this->assertTrue($result); // Load lrs from db $lrs_id = $lrs->_id; $db_lrs = Lrs::find($lrs_id); $this->assertEquals($db_lrs->_id, $lrs->_id); // Edit lrs $title = \app\locker\helpers\Helpers::getRandomValue(); $db_lrs->title = $title; $db_lrs->save(); $this->assertEquals($db_lrs->title, $title); // Delete lrs $db_lrs->delete(); $this->assertEquals(Lrs::find($lrs_id), NULL, 'delete lrs'); }
public function delete($id) { $user = $this->find($id); //get a super admin user $super = \User::where('role', 'super')->first(); //get all LRSs owned by user being deleted $get_lrs = \Lrs::where('owner._id', $id)->get(); //do LRS exists? if ($get_lrs) { foreach ($get_lrs as &$lrs) { //grab existing users $existing = $lrs->users; //add super admin as the admin user array_push($existing, array('_id' => $super->_id, 'email' => $super->email, 'role' => 'admin')); //add merged users $lrs->users = $existing; //set owner to super admin $lrs->owner = array('_id' => $super->_id); $lrs->save(); } } //remove users from any LRSs they are a member off \DB::table('lrs')->pull('users', array('_id' => $user->_id)); //delete user document return $user->delete(); }
/** * Is a user, a member of an LRS? * * @param $string $lrs * @param $string $user * * @return boolean * **/ public static function isMember($lrs, $user) { $isMember = \Lrs::where('users._id', $user)->where('_id', $lrs)->first(); if ($isMember) { return true; } return false; }
/** * Get the LRS details based on Auth credentials * **/ public function getLrs() { //get the lrs $key = \Request::getUser(); $secret = \Request::getPassword(); $lrs = \Lrs::where('api.basic_key', $key)->where('api.basic_secret', $secret)->first(); $this->lrs = $lrs; }
/** * Create dummy LRS * @return \Lrs */ protected function createLRS() { $lrs = new Lrs(); $lrs->title = helpers::getRandomValue(); $lrs->description = helpers::getRandomValue(); $lrs->subdomain = helpers::getRandomValue(); $lrs->api = array('basic_key' => helpers::getRandomValue(), 'basic_secret' => helpers::getRandomValue()); // $lrs->auth_service = property_exists($this, 'lrsAuthMethod') ? $this->lrsAuthMethod : Lrs::INTERNAL_LRS; // $lrs->auth_service_url = property_exists($this, 'auth_service_url') ? // $this->auth_service_url : ''; // $lrs->token = 'our-token'; $lrs->owner = array('_id' => Auth::user()->_id); $lrs->users = array(array('_id' => Auth::user()->_id, 'email' => Auth::user()->email, 'name' => Auth::user()->name, 'role' => 'admin')); $lrs->save(); $this->lrs = $lrs; // Hack header request $_SERVER['SERVER_NAME'] = $this->lrs->title . '.com.vn'; return $lrs; }
public function down() { $db = \DB::getMongoDB(); Lrs::get()->each(function (Lrs $lrs) use($db) { $users = $lrs->getAttribute('users'); foreach ($users as &$user) { $user['_id'] = (string) $user['_id']; } $lrs->setAttribute('users', $users); $lrs->owner_id = (string) $lrs->owner_id; $lrs->save(); echo 'IDs for lrs collection "' . $lrs->title . '" converted to strings.' . PHP_EOL; }); }
public function down() { $db = \DB::getMongoDB(); Lrs::get()->each(function (Lrs $lrs) use($db) { $convertToString = function ($value) { return (string) $value; }; $this->changeForeignKey($db->statements, 'lrs_id', 'lrs._id', $lrs->_id, $convertToString); $this->changeForeignKey($db->documentapi, 'lrs_id', 'lrs', $lrs->_id, $convertToString); $this->changeForeignKey($db->reports, 'lrs_id', 'lrs', $lrs->_id, $convertToString); $this->changeForeignKey($db->exports, 'lrs_id', 'lrs', $lrs->_id, $convertToString); $lrs->owner = ['_id' => $convertToString($lrs->owner_id)]; $lrs->save(); echo 'Models for "' . $lrs->title . '" converted.' . PHP_EOL; }); echo 'All finished, hopefully!' . PHP_EOL; }
protected function createLrs(\User $user) { $model = new \Lrs(['title' => 'Test', 'owner_id' => $user->_id, 'users' => [['_id' => $user->_id, 'email' => $user->email, 'name' => $user->name, 'role' => 'admin']]]); $model->save(); return $model; }
App::singleton('oauth2', function () { $storage = new OAuth2\Storage\Mongo(App::make('db')->getMongoDB()); $server = new OAuth2\Server($storage); $server->addGrantType(new OAuth2\GrantType\ClientCredentials($storage)); return $server; }); Route::get('/', function () { if (Auth::check()) { $site = \Site::first(); $admin_dashboard = new \app\locker\data\dashboards\AdminDashboard(); //if super admin, show site dashboard, otherwise show list of LRSs can access if (Auth::user()->role == 'super') { $list = Lrs::all(); return View::make('partials.site.dashboard', array('site' => $site, 'list' => $list, 'stats' => $admin_dashboard->getFullStats(), 'graph_data' => $admin_dashboard->getGraphData(), 'dash_nav' => true)); } else { $lrs = Lrs::where('users._id', \Auth::user()->_id)->get(); return View::make('partials.lrs.list', array('lrs' => $lrs, 'list' => $lrs, 'site' => $site)); } } else { $site = \Site::first(); if (isset($site)) { return View::make('system.forms.login', array('site' => $site)); } else { return View::make('system.forms.register'); } } }); /* |------------------------------------------------------------------ | Login |------------------------------------------------------------------
/** * test query analytics api. * * @return void */ public function testQueryAnalytics() { //testing response format $response = $this->call('GET', '/api/v1/query/analytics', array(), array(), array('PHP_AUTH_USER' => $this->lrs->api['basic_key'], 'PHP_AUTH_PW' => $this->lrs->api['basic_secret'])); $data = $response->getData(); $this->assertEquals($data->version, 'v1'); $this->assertEquals($data->route, 'api/v1/query/analytics'); //FAILURES //testing params: filter // $filter = array( // 'object.definition.type' => 'http://activitystrea.ms/schema/2.0/badge' // ); // $response = $this->call('GET', '/api/v1/query/analytics', // array('filters' => json_encode($filter)), // array(), // array('PHP_AUTH_USER' => $this->lrs->api['basic_key'], // 'PHP_AUTH_PW' => $this->lrs->api['basic_secret'])); // $data = $response->getData()->data; // var_dump($data); //testing params: type:time $response = $this->call('GET', '/api/v1/query/analytics', array('type' => 'time'), array(), array('PHP_AUTH_USER' => $this->lrs->api['basic_key'], 'PHP_AUTH_PW' => $this->lrs->api['basic_secret'])); $data = $response->getData()->data; $this->assertEquals($data[0]->count, 2); //testing params: type:user $response = $this->call('GET', '/api/v1/query/analytics', array('type' => 'user'), array(), array('PHP_AUTH_USER' => $this->lrs->api['basic_key'], 'PHP_AUTH_PW' => $this->lrs->api['basic_secret'])); $data = $response->getData()->data; $checkTypeUser = TRUE; foreach ($data as $value) { if (!in_array($value->data->name, array('quanvm', 'quanvm2'))) { $checkTypeUser = FALSE; } } $this->assertTRUE($checkTypeUser); //testing params: type:verb $response = $this->call('GET', '/api/v1/query/analytics', array('type' => 'verb'), array(), array('PHP_AUTH_USER' => $this->lrs->api['basic_key'], 'PHP_AUTH_PW' => $this->lrs->api['basic_secret'])); $data = $response->getData()->data; $this->assertEquals($data[0]->data->id, "http://adlnet.gov/expapi/verbs/experienced"); /** * testing params: type:interval required type = time * need an static LRS and updated statement for this test */ $intervalDayLrs = Lrs::find('536b02d4c01f1325618b4567'); if ($intervalDayLrs) { $response = $this->call('GET', '/api/v1/query/analytics', array('interval' => 'Day'), array(), array('PHP_AUTH_USER' => $intervalDayLrs->api['basic_key'], 'PHP_AUTH_PW' => $intervalDayLrs->api['basic_secret'])); $data = $response->getData()->data; //need 2 statements which different day $this->assertEquals(count($data), 2); } $intervalMonthLrs = Lrs::find('536b03bbc01f13a6618b4567'); if ($intervalMonthLrs) { $response = $this->call('GET', '/api/v1/query/analytics', array('interval' => 'Month'), array(), array('PHP_AUTH_USER' => $intervalMonthLrs->api['basic_key'], 'PHP_AUTH_PW' => $intervalMonthLrs->api['basic_secret'])); $data = $response->getData()->data; //need 2 statements which different Month $this->assertEquals(count($data), 2); } // FAILURES // $intervalYearLrs = Lrs::find('536b057dc01f1377638b4567'); // if ($intervalYearLrs) { // $response = $this->call('GET', '/api/v1/query/analytics', // array('interval' => 'Year'), // array(), // array('PHP_AUTH_USER' => $intervalYearLrs->api['basic_key'], // 'PHP_AUTH_PW' => $intervalYearLrs->api['basic_secret'])); // $data = $response->getData()->data; // var_dump($data); // //need 2 statement which different Year // $this->assertEquals(count($data), 2); // } $intervalWeekLrs = Lrs::find('536b05ccc01f1392638b4567'); if ($intervalWeekLrs) { $response = $this->call('GET', '/api/v1/query/analytics', array('interval' => 'Year'), array(), array('PHP_AUTH_USER' => $intervalWeekLrs->api['basic_key'], 'PHP_AUTH_PW' => $intervalWeekLrs->api['basic_secret'])); $data = $response->getData()->data; //need 2 statements which different Week $this->assertEquals(count($data), 2); } //testing params: since $response = $this->call('GET', '/api/v1/query/analytics', array('since' => date('Y-m-d')), array(), array('PHP_AUTH_USER' => $this->lrs->api['basic_key'], 'PHP_AUTH_PW' => $this->lrs->api['basic_secret'])); $data = $response->getData()->data; $this->assertEquals($data[0]->count, 2); $response = $this->call('GET', '/api/v1/query/analytics', array('since' => date('Y-m-d', strtotime("+1 day"))), array(), array('PHP_AUTH_USER' => $this->lrs->api['basic_key'], 'PHP_AUTH_PW' => $this->lrs->api['basic_secret'])); $data = $response->getData()->data; $this->assertTrue(empty($data)); //testing params: until $response = $this->call('GET', '/api/v1/query/analytics', array('until' => date('Y-m-d', strtotime("+1 day"))), array(), array('PHP_AUTH_USER' => $this->lrs->api['basic_key'], 'PHP_AUTH_PW' => $this->lrs->api['basic_secret'])); $data = $response->getData()->data; $this->assertEquals($data[0]->count, 2); $response = $this->call('GET', '/api/v1/query/analytics', array('until' => date('Y-m-d', strtotime("-1 day"))), array(), array('PHP_AUTH_USER' => $this->lrs->api['basic_key'], 'PHP_AUTH_PW' => $this->lrs->api['basic_secret'])); $data = $response->getData()->data; $this->assertTrue(empty($data)); }
/** * Add a password - this is used when users are invited into the platform. * **/ public function addPasswordForm() { $lrs_list = Lrs::all(); return View::make('partials.users.addPassword', array('user' => Auth::user(), 'lrs_list' => $lrs_list)); }
/** * Invite in a user. **/ public static function inviteUser($data) { //explode email addresses $emails = explode("\r\n", $data['emails']); foreach ($emails as $e) { $isMember = false; //make sure lower case $e = strtolower($e); //check it is a valid email address if (filter_var($e, FILTER_VALIDATE_EMAIL)) { //does the user already exist? If so, skip next step $user = \User::where('email', $e)->first(); $user_exists = false; //boolean used to determine if add to lrs email sent if (!$user) { //create a user account $user = new \User(); $user->name = $e; $user->email = $e; $user->verified = 'no'; $user->role = $data['role'] ? $data['role'] : 'observer'; $user->password = \Hash::make(base_convert(uniqid('pass', true), 10, 36)); $user->save(); } else { $user_exists = true; } //was an LRS id passed? If so, add user to that LRS as an observer if (isset($data['lrs'])) { $lrs = \Lrs::find($data['lrs']); //is the user already a member of the LRS? $isMember = \Locker\Helpers\Lrs::isMember($lrs->_id, $user->_id); //if lrs exists and user is not a member, add them if ($lrs && !$isMember) { $existing = $lrs->users; array_push($existing, array('_id' => $user->_id, 'email' => $user->email, 'role' => 'observer')); $lrs->users = $existing; $lrs->save(); } } //if user is already a member, exit here if ($isMember) { continue; } //determine which message to send to the user if ($user_exists && isset($lrs)) { //set data to use in email $set_data = array('sender' => \Auth::user(), 'lrs' => $lrs); //send out message to user \Mail::send('emails.lrsInvite', $set_data, function ($message) use($user) { $message->to($user->email, $user->name)->subject('You have been added to an LRS.'); }); } elseif ($user_exists) { //do nothing as they are already in the system } else { //if adding to lrs, get lrs title, otherwise use the site name isset($lrs) ? $title = 'the ' . $lrs->title . ' LRS' : ($title = \Site::first()->name . '\'s Learning Locker'); //set data to use in email $set_data = array('token' => User::setEmailToken($user, $user->email), 'custom_message' => $data['message'], 'title' => $title, 'sender' => \Auth::user()); //send out message to user \Mail::send('emails.invite', $set_data, function ($message) use($user) { $message->to($user->email, $user->name)->subject('You have been invited to join our LRS.'); }); } } } }
public function __construct() { $this->lrs = Lrs::all(); }
} //check current user is in the list of allowed users, or is super admin if (!in_array($user->_id, $get_users) && $user->role != 'super') { return Redirect::to('/'); } } else { return Redirect::to('/'); } }); // Checks for LRS edit access. Route::filter('edit.lrs', function ($route, $request) { //check to see if lrs id exists? $lrs = Lrs::find($route->parameter('id')); //if not, let's try the lrs parameter if (!$lrs) { $lrs = Lrs::find($route->parameter('lrs')); } $user = \Auth::user(); if ($lrs) { //get all users with admin access to the lrs foreach ($lrs->users as $u) { if ($u['role'] == 'admin') { $get_users[] = $u['_id']; } } //check current user is in the list of allowed users or is super if (!in_array($user->_id, $get_users) && $user->role != 'super') { return Redirect::to('/'); } } else { return Redirect::to('/');