CKEditor AI plugin button available without permission to use it

Created on 26 March 2025, about 1 month ago

Thanks for this great module!

I noticed that the AI assistant button is always visible in CKEditor, even for users who donโ€™t have permission to use the AI plugin. While the plugin doesnโ€™t function in those cases, the button's presence is confusing and leads to an error.

Currently, I donโ€™t see a straightforward way to configure access so that only specific roles can use the AI plugin. Adjusting the text filter to enable the AI plugin wouldnโ€™t be a practical solution.

In our setup, users create and edit content, while moderators review, refine, and approve it. Only moderators should have access to the AI plugin, but requiring them to switch text formats would be cumbersome.

Would it be possible to hide the AI assistant button when the user does not have the permission to use it?

โœจ Feature request
Status

Active

Version

1.0

Component

AI CKEditor

Created by

๐Ÿ‡ณ๐Ÿ‡ฑNetherlands ecvandenberg

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @ecvandenberg
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom MrDaleSmith

    The CKEditor plugins don't appear to have a concept of access at the plugin level, so this would probably need to be handled in the aiui.js javascript file, which would need to be able to check the permissions of the current user.

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands ecvandenberg

    For now as a work around I added the logged in user role as a body class and hide the button for those without the required role :-)

  • First commit to issue fork.
  • Pipeline finished with Failed
    about 1 month ago
    Total: 384s
    #458172
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom MrDaleSmith

    You have failing tests. I thought it might be better to address the issue within the existing JS, but I think this is a fairly elegant solution.

  • Pipeline finished with Success
    about 1 month ago
    Total: 230s
    #458900
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia anjaliprasannan

    Thank you mrdalesmith
    Pipeline is passed. Moving to Needs review

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands ecvandenberg

    Thanks for the quick action!

    I tried the patch, cleared all cache, rebuild the permissions, cleared browser cache but the button is still there for users without the permission to use it.

    Do I miss something?

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom MrDaleSmith

    Yeah the patch doesn't work because the form_alter only adds the library to the filter_format_edit_form form, so the javascript never makes it onto the page when content using CKEditor is being created/edited.

    I think this may need a rethink @anjaliprasannan - all you're doing is hiding it when the filters are being edited by admins.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia anjaliprasannan

    Its my bad, I was mistaken that the ticket was to remove the ai plugin icon in the configurations. Now the patch is updated to remove the ai assistant dropdown in ckeditor.

    User with ckeditor permission

    User without ckeditor permission

  • Pipeline finished with Success
    about 1 month ago
    Total: 3346s
    #462132
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom MrDaleSmith

    Couple of suggestions to improve the code.

  • Pipeline finished with Success
    about 1 month ago
    Total: 225s
    #462545
  • Pipeline finished with Success
    about 1 month ago
    Total: 227s
    #463170
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom MrDaleSmith

    There seem to be some commits relating to AI Assistant Block access that have leaked into this MR.

  • Pipeline finished with Success
    about 1 month ago
    Total: 300s
    #463981
  • Pipeline finished with Success
    about 1 month ago
    Total: 295s
    #463987
  • Pipeline finished with Success
    about 1 month ago
    Total: 262s
    #463991
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom MrDaleSmith

    Still not working for me, I'm afraid. Steps to recreate:

    1. Clean site install.
    2. Enable AI CKEditor with patch above.
    3. Update Full HTML text format to use AI CKEditor. Ensure Basic HTML does NOT allow use of CK Editor.
    4. Create content type with WYSIWYG field that uses Basic HTML as default format.
    5. Create user with permission to use Basic HTML and Full HTML but NOT permission to use AK CKEditor
    6. Log in as user and create content with new content type.

    The code in the form alter runs when the form is first opened and correctly identifies that a WYSIWYG field is in use and that the logged in user does not have the required permission. However, switching the field's text format from the default Basic HTML (no AI) to Full HTML (uses AI) results in the AI button appearing. There are no errors in console or in watchdog.

    The form alter also runs now on every form on the site, even those that can never have CK Editor in use: I think this might be better if the access is checked within the existing CK Editor javascript so that it only runs when CK Editor is present.

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands ecvandenberg

    I just tried the latest MR and it works in my config because the user that lacks the permission to use the AI function only has one Text filter available. So no switching in text filters.

    Indeed, if you give a user permission to use more than one text filter the AI button appears after switching the filter. But a page refresh removes the button again.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom MrDaleSmith

    Setting back to needs work: the previous comment confirms it doesn't work if the user has access to multiple text formats, so this is not ready to merge. Also needs to address bringing the solution inside the existing JS, and removing the need for the form alter.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany marcus_johansson

    @anjaliprasannan - Sorry, just saw that this issue was sending mails back and forth. I think that if you have form_alters that runs on every form with checkups and intervals that might be started on multiple ckeditor fields you can run into performance issues.

    I think you need to solve it via the aiui.js and the AiCKEditor.php plugin, since they are both the insantiators of the backend and the frontend.

    Check this patch: https://www.drupal.org/files/issues/2025-04-03/ai_ckeditor_hide.patch โ†’ - I think that is mostly how it should be solved. Could you check that one and see if that works for you and if you agree with the solution, try to create a (maybe improved) MR with it?

    @paul - maybe check if this is close to how you initially thought you would solve it?

    Thanks!

  • Pipeline finished with Success
    about 1 month ago
    Total: 217s
    #465151
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia anjaliprasannan

    @marcus_johansson I tried the patch and it works well! I've incorporated the changes and improved in the MR.

    Let me know if you have any feedback or further suggestions.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom MrDaleSmith

    Yep, looks better and works better :)

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany marcus_johansson

    Great!

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands ecvandenberg

    Works well here too. Even with switching text formats. Thanks!

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany marcus_johansson

    This got a merge conflict that I fixed and rebuilt it, will get merged. Thanks everyone!

Production build 0.71.5 2024