Commit 5580903d authored by Aaron Wells's avatar Aaron Wells
Browse files

Decoupling "copy to new users" settings from other settings

Bug 1267633: Currently the settings for forcing a page to be copied
to new users, new groups, and new institution members, are only available
if the page is set to be copyable and is viewable to logged-in users.

But logically there's no reason to link those settings together.

Change-Id: I68b4579d891a56e617a04947664d01d59e620bdf
parent e1905351
......@@ -516,7 +516,7 @@ class User {
'title' => get_field('view', 'title', 'id', $systemprofileviewid),
'description' => get_string('profiledescription'),
'type' => 'profile',
), $systemprofileviewid, $this->get('id'));
), $systemprofileviewid, $this->get('id'), false);
// Add about me block
$aboutme = new BlockInstance(0, array(
......@@ -592,7 +592,7 @@ class User {
'title' => get_field('view', 'title', 'id', $systemdashboardviewid),
'description' => get_string('dashboarddescription'),
'type' => 'dashboard',
), $systemdashboardviewid, $this->get('id'));
), $systemdashboardviewid, $this->get('id'), false);
db_commit();
......@@ -1203,10 +1203,8 @@ class User {
* Makes a literal copy of a list of views and collections for the new user.
* All site views and collections which set to "copy to new user"
* will be copied to this user's profile.
*
* @param $checkviewaccess.
*/
public function copy_site_views_collections_to_new_user($checkviewaccess=true) {
public function copy_site_views_collections_to_new_user() {
// Get list of available views which are not in collections
$templateviewids = get_column_sql("
SELECT v.id
......@@ -1215,7 +1213,7 @@ class User {
WHERE cv.view IS NULL
AND v.institution = 'mahara'
AND v.copynewuser = 1", array());
$this->copy_views($templateviewids, $checkviewaccess);
$this->copy_views($templateviewids, false);
// Get list of available collections
$templatecollectionids = get_column_sql("
......@@ -1228,7 +1226,7 @@ class User {
if ($templatecollectionids) {
require_once('collection.php');
foreach ($templatecollectionids as $templateid) {
Collection::create_from_template(array('owner' => $this->get('id')), $templateid, null, null, true);
Collection::create_from_template(array('owner' => $this->get('id')), $templateid, null, false, true);
}
}
}
......@@ -1239,9 +1237,8 @@ class User {
* will be copied to this user's profile.
*
* @param $institution: ID of the institution to join
* @param $checkviewaccess.
*/
public function copy_institution_views_collections_to_new_member($institution, $checkviewaccess=true) {
public function copy_institution_views_collections_to_new_member($institution) {
if (empty($institution)) {
return;
}
......@@ -1253,7 +1250,7 @@ class User {
WHERE cv.view IS NULL
AND v.institution = ?
AND v.copynewuser = 1", array($institution));
$this->copy_views($templateviewids, $checkviewaccess);
$this->copy_views($templateviewids, false);
// Get list of available collections
$templatecollectionids = get_column_sql("
......@@ -1266,7 +1263,7 @@ class User {
if ($templatecollectionids) {
require_once('collection.php');
foreach ($templatecollectionids as $templateid) {
Collection::create_from_template(array('owner' => $this->get('id')), $templateid, null, null, true);
Collection::create_from_template(array('owner' => $this->get('id')), $templateid, null, false, true);
}
}
}
......
......@@ -468,7 +468,7 @@ function group_create($data) {
'group' => $id,
'title' => $template->title,
'description' => $template->description,
), $template->id);
), $template->id, null, false);
$view->set_access(array(array(
'type' => 'group',
'id' => $id,
......@@ -489,7 +489,7 @@ function group_create($data) {
if ($templates) {
require_once('collection.php');
foreach ($templates as $template) {
Collection::create_from_template(array('group' => $id), $template->id, null, null, true);
Collection::create_from_template(array('group' => $id), $template->id, null, false, true);
}
}
$data['id'] = $id;
......
......@@ -334,7 +334,7 @@ class Institution {
$checkviewaccess = empty($user->newuser) && !$USER->get('admin');
$userobj = new User();
$userobj->find_by_id($user->id);
$userobj->copy_institution_views_collections_to_new_member($this->name, $checkviewaccess);
$userobj->copy_institution_views_collections_to_new_member($this->name);
require_once('activity.php');
activity_occurred('maharamessage', $message);
handle_event('updateuser', $userinst->usr);
......
......@@ -2140,8 +2140,6 @@ function create_user($user, $profile=array(), $institution=null, $remoteauth=nul
}
$user->id = insert_record('usr', $user, 'id', true);
}
// Bypass access check for 'copynewuser' institution/site views, because this user may not be logged in yet
$user->newuser = true;
if (isset($user->email) && $user->email != '') {
set_profile_field($user->id, 'email', $user->email);
......@@ -2201,10 +2199,9 @@ function create_user($user, $profile=array(), $institution=null, $remoteauth=nul
}
// Copy site views and collections to the new user's profile
$checkviewaccess = !$user->newuser;
$userobj = new User();
$userobj->find_by_id($user->id);
$userobj->copy_site_views_collections_to_new_user($checkviewaccess);
$userobj->copy_site_views_collections_to_new_user();
reset_password($user, false, $quickhash);
......
......@@ -365,7 +365,7 @@ class View {
throw new SystemException("View::create_from_template: This template has been deleted");
}
if (!$template->get('template') && !$user->can_edit_view($template)) {
if ($checkaccess && !$template->get('template') && !$user->can_edit_view($template)) {
throw new SystemException("View::create_from_template: Attempting to create a View from another View that is not marked as a template");
}
else if ($checkaccess && !can_view_view($templateid, $userid)) {
......
......@@ -175,7 +175,7 @@ if ($institution) {
'type' => 'checkbox',
'title' => get_string('copyfornewusers', 'view'),
'description' => get_string('copyfornewusersdescription1', 'view'),
'defaultvalue' => $view->get('template') && $view->get('copynewuser'),
'defaultvalue' => $view->get('copynewuser'),
);
$form['elements']['more']['elements']['copyfornewgroups'] = array(
'type' => 'html',
......@@ -185,17 +185,13 @@ if ($institution) {
'type' => 'html',
'value' => '<div class="description">' . get_string('copyfornewgroupsdescription1', 'view') . '</div>',
);
$copyoptions = array('copynewuser', 'copyfornewgroups', 'copyfornewgroupsdescription1');
$needsaccess = array('copynewuser');
$createfor = $view->get_autocreate_grouptypes();
foreach (group_get_grouptype_options() as $grouptype => $grouptypedesc) {
$form['elements']['more']['elements']['copyfornewgroups_'.$grouptype] = array(
'type' => 'checkbox',
'title' => $grouptypedesc,
'defaultvalue' => $view->get('template') && in_array($grouptype, $createfor),
'defaultvalue' => in_array($grouptype, $createfor),
);
$copyoptions[] = 'copyfornewgroups_'.$grouptype;
$needsaccess[] = 'copyfornewgroups_'.$grouptype;
}
}
else {
......@@ -206,46 +202,9 @@ if ($institution) {
'type' => 'checkbox',
'title' => get_string('copyfornewmembers', 'view'),
'description' => get_string('copyfornewmembersdescription1', 'view', $instname),
'defaultvalue' => $view->get('template') && $view->get('copynewuser'),
'defaultvalue' => $view->get('copynewuser'),
);
$copyoptions = array('copynewuser');
$needsaccess = array('copynewuser');
}
$copyoptionstr = json_encode($copyoptions);
$needsaccessstr = json_encode($needsaccess);
$js .= <<<EOF
function update_copy_options() {
if ($('editaccess_template').checked) {
forEach({$copyoptionstr}, function (id) {
removeElementClass($('editaccess_'+id+'_container'), 'hidden');
});
}
else {
forEach({$copyoptionstr}, function (id) {
addElementClass($('editaccess_'+id+'_container'), 'hidden');
});
forEach({$needsaccessstr}, function (id) {
$('editaccess_'+id).checked = false;
});
update_loggedin_access();
}
}
function update_loggedin_access() {
if (some({$needsaccessstr}, function (id) { return $('editaccess_'+id).checked; })) {
ensure_loggedin_access();
}
else {
relax_loggedin_access();
}
}
addLoadEvent(function() {
update_copy_options();
connect('editaccess_template', 'onclick', update_copy_options);
forEach({$needsaccessstr}, function (id) {
connect('editaccess_'+id, 'onclick', update_loggedin_access);
});
});
EOF;
} else {
$form['elements']['more']['elements']['retainview'] = array(
'type' => 'checkbox',
......@@ -385,21 +344,7 @@ function ptimetotime($ptime) {
function editaccess_validate(Pieform $form, $values) {
global $SESSION, $institution, $group;
if ($institution && $values['copynewuser'] && !$values['template']) {
$form->set_error('copynewuser', get_string('viewscopiedfornewusersmustbecopyable', 'view'));
}
$createforgroup = false;
if ($institution == 'mahara') {
foreach (group_get_grouptypes() as $grouptype) {
if ($values['copyfornewgroups_'.$grouptype]) {
$createforgroup = true;
break;
}
}
if ($createforgroup && !$values['template']) {
$form->set_error('copyfornewgroups', get_string('viewscopiedfornewgroupsmustbecopyable', 'view'));
}
}
$retainview = isset($values['retainview']) ? $values['retainview'] : false;
if ($retainview && !$values['template']) {
$form->set_error('retainview', get_string('viewswithretainviewrightsmustbecopyable', 'view'));
......@@ -453,12 +398,6 @@ function editaccess_validate(Pieform $form, $values) {
}
}
}
// Must have logged in user access for copy new user/group settings.
if (($createforgroup || ($institution && $values['copynewuser'])) && !$loggedinaccess) {
$SESSION->add_error_msg(get_string('copynewusergroupneedsloggedinaccess', 'view'));
$form->set_error('accesslist', '');
}
}
if (!empty($institution)) {
......@@ -511,12 +450,12 @@ function editaccess_submit(Pieform $form, $values) {
if ($institution) {
if (isset($values['copynewuser'])) {
$viewconfig['copynewuser'] = (int) ($values['template'] && $values['copynewuser']);
$viewconfig['copynewuser'] = (int) $values['copynewuser'];
}
if ($institution == 'mahara') {
$createfor = array();
foreach (group_get_grouptypes() as $grouptype) {
if ($values['template'] && $values['copyfornewgroups_'.$grouptype]) {
if ($values['copyfornewgroups_'.$grouptype]) {
$createfor[] = $grouptype;
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment