@@ -161,29 +161,7 @@ public static function moreRightsForWidgetManager(\Elgg\Event $event): void {
161161
162162 if ($ widget ->canEdit ()) {
163163 // the widgets action might not be able to get privately owned index widgets
164- elgg_register_event_handler ('get_sql ' , 'access ' , function (\Elgg \Event $ event ) use ($ widget_guid ) {
165- if ($ event ->getParam ('ignore_access ' )) {
166- // no need to give extra access
167- return null ;
168- }
169-
170- /**
171- * @var QueryBuilder $qb
172- */
173- $ qb = $ event ->getParam ('query_builder ' );
174- $ table_alias = $ event ->getParam ('table_alias ' );
175- $ guid_column = $ event ->getParam ('guid_column ' );
176-
177- $ alias = function ($ column ) use ($ table_alias ) {
178- return $ table_alias ? "{$ table_alias }. {$ column }" : $ column ;
179- };
180-
181- $ result = $ event ->getValue ();
182-
183- $ result ['ors ' ]['special_widget_access ' ] = $ qb ->compare ($ alias ($ guid_column ), '= ' , $ widget_guid );
184-
185- return $ result ;
186- });
164+ self ::registerSQLBypass ($ widget_guid );
187165 }
188166
189167 if ($ event ->getType () === 'widgets/move ' ) {
@@ -205,6 +183,39 @@ public static function moreRightsForWidgetManager(\Elgg\Event $event): void {
205183 }
206184 }
207185 }
186+
187+ /**
188+ * Registers a bypass sql suffix
189+ *
190+ * @param int $guid GUID of the entity to register sql bypass for
191+ *
192+ * @return void
193+ */
194+ protected static function registerSQLBypass (int $ guid ): void {
195+ elgg_register_event_handler ('get_sql ' , 'access ' , function (\Elgg \Event $ event ) use ($ guid ) {
196+ if ($ event ->getParam ('ignore_access ' )) {
197+ // no need to give extra access
198+ return null ;
199+ }
200+
201+ /**
202+ * @var QueryBuilder $qb
203+ */
204+ $ qb = $ event ->getParam ('query_builder ' );
205+ $ table_alias = $ event ->getParam ('table_alias ' );
206+ $ guid_column = $ event ->getParam ('guid_column ' );
207+
208+ $ alias = function ($ column ) use ($ table_alias ) {
209+ return $ table_alias ? "{$ table_alias }. {$ column }" : $ column ;
210+ };
211+
212+ $ result = $ event ->getValue ();
213+
214+ $ result ['ors ' ]['special_widget_access ' ] = $ qb ->compare ($ alias ($ guid_column ), '= ' , $ guid );
215+
216+ return $ result ;
217+ });
218+ }
208219
209220 /**
210221 * Only allow widget page delete by admins
@@ -223,4 +234,30 @@ public static function onlyAdminsCanDeleteWidgetPages(\Elgg\Event $event) {
223234
224235 return $ user ->isAdmin ();
225236 }
237+
238+ /**
239+ * Only allow widget edit for private widgets
240+ *
241+ * @param \Elgg\Event $event 'view_vars', 'object/widget/edit'
242+ *
243+ * @return null|array
244+ */
245+ public static function allowPrivateWidgetEdit (\Elgg \Event $ event ): ?array {
246+ $ result = $ event ->getValue ();
247+ if (elgg_extract ('entity ' , $ result ) instanceof \ElggEntity) {
248+ return $ result ;
249+ }
250+
251+ $ guid = (int ) elgg_extract ('guid ' , $ result );
252+ $ entity = elgg_call (ELGG_IGNORE_ACCESS , function () use ($ guid ) {
253+ return get_entity ($ guid );
254+ });
255+
256+ if ($ entity ->canEdit ()) {
257+ $ result ['entity ' ] = $ entity ;
258+ self ::registerSQLBypass ($ guid );
259+ }
260+
261+ return $ result ;
262+ }
226263}
0 commit comments