Skip to content

Commit c84958d

Browse files
author
Tamara Gunkel
committed
improved permission check for ownership
1 parent 745e9c2 commit c84958d

File tree

10 files changed

+108
-32
lines changed

10 files changed

+108
-32
lines changed

changeowner.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@
4646
$systemcontext = context_system::instance();
4747

4848
if ($courseid == $SITE->id) {
49-
course_require_view_participants($systemcontext);
49+
require_capability('block/opencast:viewusers', $systemcontext);
50+
$viewfullnames = has_capability('moodle/site:viewfullnames', $systemcontext);
5051
} else {
5152
course_require_view_participants($coursecontext);
53+
$viewfullnames = has_capability('moodle/site:viewfullnames', $coursecontext);
5254
}
5355

54-
if (empty(get_config('aclownerrole_' . $ocinstanceid, 'block_opencast'))) {
56+
if (empty(get_config('block_opencast', 'aclownerrole_' . $ocinstanceid))) {
5557
redirect($redirecturl, get_string('functionalitydisabled', 'block_opencast'), null,
5658
\core\output\notification::NOTIFY_ERROR);
5759
}
@@ -66,6 +68,7 @@
6668
}
6769
$title = $series->title;
6870
$acls = $series->acl;
71+
$noowner = !$apibridge->has_owner($series->acl);
6972

7073
} else {
7174
$video = $apibridge->get_opencast_video($identifier, false, true);
@@ -77,10 +80,20 @@
7780
$title = $video->video->title;
7881
$acls = $video->video->acl;
7982
}
83+
$noowner = !$apibridge->has_owner($acls);
84+
if ($noowner) {
85+
// Check if user owns series.
86+
$series = $apibridge->get_series_by_identifier($video->video->is_part_of, true);
87+
if (!$series || (!$apibridge->is_owner($acls, $USER->id, $courseid) && $apibridge->has_owner($series->acl))) {
88+
$noowner = false;
89+
}
90+
}
8091
}
8192

8293
// Verify that current user is the owner or is admin.
83-
if (!$apibridge->is_owner($acls, $USER->id, $courseid) &&
94+
$isowner = $apibridge->is_owner($acls, $USER->id, $courseid);
95+
if (!$isowner &&
96+
!$noowner &&
8497
!has_capability('block/opencast:canchangeownerforallvideos', $systemcontext)) {
8598
throw new moodle_exception(get_string('userisntowner', 'block_opencast'));
8699
} else {
@@ -90,12 +103,18 @@
90103
$PAGE->navbar->add(get_string('pluginname', 'block_opencast'), $redirecturl);
91104
$PAGE->navbar->add(get_string('changeowner', 'block_opencast'), $baseurl);
92105

106+
$excludeusers = array();
107+
if ($isowner) {
108+
$excludeusers = [$USER->id];
109+
}
110+
93111
$userselector = new block_opencast_enrolled_user_selector('ownerselect',
94-
array('context' => $coursecontext, 'multiselect' => false));
112+
array('context' => $coursecontext, 'multiselect' => false, 'exclude' => $excludeusers));
113+
$userselector->viewfullnames = $viewfullnames;
95114

96115
$changeownerform = new \block_opencast\local\changeowner_form(null,
97116
array('courseid' => $courseid, 'title' => $title, 'identifier' => $identifier,
98-
'ocinstanceid' => $ocinstanceid, 'userselector' => $userselector, 'isseries' => $isseries));
117+
'ocinstanceid' => $ocinstanceid, 'userselector' => $userselector, 'isseries' => $isseries, 'noowner' => $noowner));
99118

100119
if ($changeownerform->is_cancelled()) {
101120
redirect($redirecturl);

classes/local/apibridge.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,6 +2103,37 @@ public function is_owner($acls, $userid, $courseid) {
21032103
return in_array($roletosearch, $acls);
21042104
}
21052105

2106+
/**
2107+
* Checks if a given event/series has an owner.
2108+
* @param string[] $acls ACLs
2109+
* @return bool
2110+
*/
2111+
public function has_owner($acls) {
2112+
$ownerrole = get_config('block_opencast', 'aclownerrole_' . $this->ocinstanceid);
2113+
$ownerroleregex = false;
2114+
foreach (self::$userplaceholders as $userplaceholder) {
2115+
$r = str_replace($userplaceholder, '.*?', $ownerrole);
2116+
if ($r != $ownerrole) {
2117+
$ownerroleregex = $r;
2118+
break;
2119+
}
2120+
}
2121+
2122+
if (!$ownerroleregex) {
2123+
return false;
2124+
}
2125+
2126+
$ownerroleregex = '/' . $ownerroleregex . '/';
2127+
2128+
foreach (array_column($acls, 'role') as $role) {
2129+
if (preg_match($ownerroleregex, $role)) {
2130+
return true;
2131+
}
2132+
}
2133+
2134+
return false;
2135+
}
2136+
21062137
/**
21072138
* Returns the owner rolename for a given user.
21082139
* @param int $userid

classes/local/changeowner_form.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,21 @@ public function definition() {
6262
$mform->setType('isseries', PARAM_BOOL);
6363

6464
if ($this->_customdata['isseries']) {
65-
$notification = $renderer->wizard_intro_notification(
66-
get_string('changeownerseries_explanation', 'block_opencast', $this->_customdata['title']));
65+
if ($this->_customdata['noowner']) {
66+
$notification = $renderer->wizard_intro_notification(
67+
get_string('claimownerseries_explanation', 'block_opencast', $this->_customdata['title']));
68+
} else {
69+
$notification = $renderer->wizard_intro_notification(
70+
get_string('changeownerseries_explanation', 'block_opencast', $this->_customdata['title']));
71+
}
6772
} else {
68-
$notification = $renderer->wizard_intro_notification(
69-
get_string('changeowner_explanation', 'block_opencast', $this->_customdata['title']));
73+
if ($this->_customdata['noowner']) {
74+
$notification = $renderer->wizard_intro_notification(
75+
get_string('claimowner_explanation', 'block_opencast', $this->_customdata['title']));
76+
} else {
77+
$notification = $renderer->wizard_intro_notification(
78+
get_string('changeowner_explanation', 'block_opencast', $this->_customdata['title']));
79+
}
7080
}
7181

7282
$mform->addElement('html', $notification);

db/access.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
),
3333
'clonepermissionsfrom' => 'moodle/my:manageblocks'
3434
),
35+
'block/opencast:viewusers' => array(
36+
'captype' => 'read',
37+
'riskbitmask' => RISK_PERSONAL,
38+
'contextlevel' => CONTEXT_SYSTEM,
39+
'archetypes' => array(
40+
'manager' => CAP_ALLOW
41+
)
42+
),
3543
'block/opencast:addinstance' => array(
3644
'riskbitmask' => RISK_SPAM,
3745
'captype' => 'write',

index.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -312,18 +312,16 @@
312312

313313

314314
foreach ($seriesvideodata as $series => $videodata) {
315-
// Get series title from first video.
316-
if ($videodata->videos && $videodata->videos[0]) {
317-
echo $renderer->render_series_intro($coursecontext, $ocinstanceid, $courseid, $series, $videodata->videos[0]->series);
315+
// Try to retrieve name from opencast.
316+
$ocseries = $apibridge->get_series_by_identifier($series, true);
317+
$isseriesowner = false;
318+
319+
if ($ocseries) {
320+
echo $renderer->render_series_intro($coursecontext, $ocinstanceid, $courseid, $series, $ocseries->title);
321+
$isseriesowner = $opencast->is_owner($ocseries->acl, $USER->id, $courseid) || !$opencast->has_owner($ocseries->acl);
318322
} else {
319-
// Try to retrieve name from opencast.
320-
$ocseries = $apibridge->get_series_by_identifier($series);
321-
if ($ocseries) {
322-
echo $renderer->render_series_intro($coursecontext, $ocinstanceid, $courseid, $series, $ocseries->title);
323-
} else {
324-
// If that fails use id.
325-
echo $renderer->render_series_intro($coursecontext, $ocinstanceid, $courseid, $series, $series);
326-
}
323+
// If that fails use id.
324+
echo $renderer->render_series_intro($coursecontext, $ocinstanceid, $courseid, $series, $series);
327325
}
328326

329327
if ($videodata->error == 0) {
@@ -415,8 +413,10 @@
415413
$updatemetadata = $opencast->can_update_event_metadata($video, $courseid);
416414
$useeditor = $opencast->can_edit_event_in_editor($video, $courseid);
417415
$canchangeowner = ($opencast->is_owner($video->acl, $USER->id, $courseid) ||
416+
($isseriesowner && !$opencast->has_owner($video->acl)) ||
418417
has_capability('block/opencast:canchangeownerforallvideos', context_system::instance())) &&
419-
!empty(get_config('aclownerrole_' . $ocinstanceid, 'block_opencast'));
418+
!empty(get_config('block_opencast', 'aclownerrole_' . $ocinstanceid));
419+
420420
$actions .= $renderer->render_edit_functions($ocinstanceid, $courseid, $video->identifier, $updatemetadata,
421421
$workflowsavailable, $coursecontext, $useeditor, $canchangeowner);
422422

lang/en/block_opencast.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@
207207
$string['changeowner'] = 'Change owner';
208208
$string['changeowner_explanation'] = 'Currently, you are the owner of the video {$a}.<br>You can transfer the ownership to another person.<br><b>Notice:</b> You might loose the right to access the video.';
209209
$string['changeownerseries_explanation'] = 'Currently, you are the owner of the series {$a}.<br>You can transfer the ownership to another person.<br><b>Notice:</b> You might loose the right to access the series.';
210+
$string['claimowner_explanation'] = 'Currently, nobody owns the video {$a}.<br>You can claim the ownership or set another person as owner.<br><b>Notice:</b> You might loose the right to access the video if you do not claim the ownership yourself.';
211+
$string['claimownerseries_explanation'] = 'Currently, nobody owns the series {$a}.<br>You can claim the ownership or set another person as owner.<br><b>Notice:</b> You might loose the right to access the series if you do not claim the ownership yourself.';
210212
$string['changevisibility_header'] = 'Change visibility for {$a->title}';
211213
$string['changevisibility'] = 'Alter visibility';
212214
$string['connection_failure'] = 'Could not reach Opencast server.';
@@ -529,6 +531,7 @@
529531
$string['opencast:myaddinstance'] = 'Add a new opencast upload block to Dashboard';
530532
$string['opencast:unassignevent'] = 'Unassign a video from the course, where the video was uploaded.';
531533
$string['opencast:viewunpublishedvideos'] = 'View all the videos from opencast server, even when they are not published';
534+
$string['opencast:viewusers'] = 'View all users so that the series/event owner can be changed in dashboard.';
532535
$string['opencaststudiointegration'] = 'Opencast studio integration';
533536
$string['opencastseries'] = 'Opencast Series';
534537
$string['owner'] = 'Owner';

overview.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,17 @@
8787
$sortcolumns = $table->get_sort_columns();
8888

8989
$activityinstalled = \core_plugin_manager::instance()->get_plugin_info('mod_opencast') != null;
90-
$showchangeownerlink = course_can_view_participants(context_system::instance()) &&
91-
!empty(get_config('aclownerrole_' . $ocinstanceid, 'block_opencast'));
90+
$showchangeownerlink = has_capability('block/opencast:viewusers', context_system::instance()) &&
91+
!empty(get_config('block_opencast', 'aclownerrole_' . $ocinstanceid));
9292

9393
foreach ($myseries as $seriesid) {
9494
$row = array();
9595

96+
// Try to retrieve name from opencast.
97+
$ocseries = $apibridge->get_series_by_identifier($seriesid, true);
98+
9699
// Check if current user is owner of the series.
97-
if (in_array($seriesid, $ownedseries)) {
100+
if (in_array($seriesid, $ownedseries) || ($ocseries && !$apibridge->has_owner($ocseries->acl))) {
98101
if ($showchangeownerlink) {
99102
$row[] = html_writer::link(new moodle_url('/blocks/opencast/changeowner.php',
100103
array('ocinstanceid' => $ocinstanceid, 'identifier' => $seriesid, 'isseries' => true)),
@@ -107,8 +110,6 @@
107110
$row[] = '';
108111
}
109112

110-
// Try to retrieve name from opencast.
111-
$ocseries = $apibridge->get_series_by_identifier($seriesid);
112113
if ($ocseries) {
113114
$row[] = $ocseries->title;
114115
} else {

overview_videos.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@
7676
}
7777
}
7878

79+
80+
$isseriesowner = $ocseries && ($apibridge->is_owner($ocseries->acl, $USER->id, $SITE->id) || !$apibridge->has_owner($ocseries->acl));
81+
7982
$PAGE->navbar->add(get_string('opencastseries', 'block_opencast'),
8083
new moodle_url('/blocks/opencast/overview.php', array('ocinstanceid' => $ocinstanceid)));
8184
$PAGE->navbar->add(get_string('pluginname', 'block_opencast'), $baseurl);
@@ -103,11 +106,11 @@
103106

104107
$videos = $apibridge->get_series_videos($series)->videos;
105108
$activityinstalled = \core_plugin_manager::instance()->get_plugin_info('mod_opencast') != null;
106-
$showchangeownerlink = course_can_view_participants(context_system::instance()) &&
107-
!empty(get_config('aclownerrole_' . $ocinstanceid, 'block_opencast'));
109+
$showchangeownerlink = has_capability('block/opencast:viewusers', context_system::instance()) &&
110+
!empty(get_config('block_opencast', 'aclownerrole_' . $ocinstanceid));
108111

109112
foreach ($renderer->create_overview_videos_rows($videos, $apibridge, $ocinstanceid,
110-
$activityinstalled, $showchangeownerlink) as $row) {
113+
$activityinstalled, $showchangeownerlink, false, $isseriesowner) as $row) {
111114
$table->add_data($row);
112115
}
113116

renderer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ public function create_overview_videos_table($id, $headers, $columns, $baseurl)
359359
* @throws moodle_exception
360360
*/
361361
public function create_overview_videos_rows($videos, $apibridge, $ocinstanceid, $activityinstalled,
362-
$showchangeownerlink, $isownerverified = false) {
362+
$showchangeownerlink, $isownerverified = false, $isseriesowner = false) {
363363
global $USER, $SITE, $DB;
364364
$rows = array();
365365

@@ -372,7 +372,8 @@ public function create_overview_videos_rows($videos, $apibridge, $ocinstanceid,
372372

373373
$row = array();
374374

375-
if ($isownerverified || $apibridge->is_owner($video->acl, $USER->id, $SITE->id)) {
375+
if ($isownerverified || $apibridge->is_owner($video->acl, $USER->id, $SITE->id) ||
376+
($isseriesowner && !$apibridge->has_owner($video->acl))) {
376377
if ($showchangeownerlink) {
377378
$row[] = html_writer::link(new moodle_url('/blocks/opencast/changeowner.php',
378379
array('ocinstanceid' => $ocinstanceid, 'identifier' => $video->identifier, 'isseries' => false)),

version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
defined('MOODLE_INTERNAL') || die();
2525

26-
$plugin->version = 2022022200;
26+
$plugin->version = 2022022201;
2727
$plugin->requires = 2017111300;
2828
$plugin->maturity = MATURITY_STABLE;
2929
$plugin->release = 'v3.11-r7';

0 commit comments

Comments
 (0)