Disable any kind of copy.

Hello everyone,

I want to disallow some users to mark text and copy the contect. Right click->Copy or Ctrl+C
I know that this can be done via javascript but don’t know a thing about javascript.
At least if someone could point me to the right direction.

Any help will be appriciated.

If I were to suggest, this is not a good idea as such restriction can be easily by-passed with a small amount of knowledge in HTML/Js.
If you still wish to do it, I’ve got a snippet from stackoverflow which seems to work pretty good.

$(document).bind('copy', function(e) {
    alert('Copy is not allowed !!!');
    e.preventDefault();
});
$(document).bind('paste', function() {
    alert('Paste is not allowed !!!');
    e.preventDefault();
});
$(document).bind('cut', function() {
    alert('Cut is not allowed !!!');
    e.preventDefault();
});
$(document).bind('contextmenu', function(e) {
    alert('Right Click is not allowed !!!');
    e.preventDefault();
});

Include the above code in whichever views you want the events to be restricted.

1 Like

Thank you for your answer.
I know that it can by-passed, i have done it before but the management still want it.

Could you be a bit more specific of where to put the code?

Since you mentioned you want to disallow only certain users, I’m assuming this wont be a simple JS file but a PHP file and the script will run based on Access Rights.
I have a few questions

  1. Will this script be running throughout the application or only on certain views/pages?
  2. Is the script going to execute for all users or just non-admins or based on access rights?
  3. Have you tried explaining the management that this feature can be easily bypassed?
  1. The script will run throughout the app.
  2. If it is possible i would like to set the id of the user inside the php file. So it will be triggered on specific users no matter about Access Right or anything else.
  3. I did. I even mentioned your previous post and posts from stackoverflow.

Could be implemented using an application hook, like after_entry_point.?
A logic_hook would be nice.

After a bit of research this seems possible with after_ui_frame logic hook. First of all you will have to define a global logic hook in custom/Extension/application/Ext/Logichooks/.php

    $hook_version = 1;
    $hook_array = Array();

    $hook_array['after_ui_frame'] = Array();
    $hook_array['after_ui_frame'][] = Array(
        //Processing index. For sorting the array.
        23, 

        //Label. A string value to identify the hook.
        'after_ui_frame example', 

        //The PHP file where your class is located.
        'custom/modules/<your_file_name>.php', 

        //The class the method is in.
        'logic_hooks_class', 

        //The method to call.
        'after_ui_frame_method' 
    );

Next you need to create a <your_file_name>.php script in custom/modules/

<?php

    if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

    class logic_hooks_class
    {
        function after_ui_frame_method($event, $arguments)
        {
			global $current_user;
           echo "<script>
$(document).bind('copy', function(e) {
    alert('Copy is not allowed !!!');
    e.preventDefault();
});
$(document).bind('paste', function() {
    alert('Paste is not allowed !!!');
    e.preventDefault();
});
$(document).bind('cut', function() {
    alert('Cut is not allowed !!!');
    e.preventDefault();
});
$(document).bind('contextmenu', function(e) {
    alert('Right Click is not allowed !!!');
    e.preventDefault();
});
</script>";
        }
    }

?>

In the above script, use the global variable $current_user to fetch the current user’s ID or Role and just add an “if” condition.
Run a QRAR. Lemme know if it works!

1 Like

This works like a charm. QRAR is not necessary. I also commented the javascript alert commands, so the user didn’t get any messages.

EDIT:
There is a missing ‘e’ at paste and cut function. So the user is still able to paste and cut.
If you want to disable them too add an ‘e’ in function() .
They should look like this:
$(document).bind(‘paste’, function(e)
$(document).bind(‘cut’, function(e)