browserlist = itsm.getParameter("browserlist")
v = itsm.getParameter("v") # give 0 to block extensions or give 1 to unblock the extensions
import _winreg
import os
import ctypes
from subprocess import Popen,PIPE
import re

def profilechecking(walkpath,path):
    pathlist = []
    for i in fil_users:
        for root, dirs, files in os.walk(walkpath%(i)):
            for e in dirs:
                if "Default" or "Profile" in e:
                    if os.path.exists(path%(i,e)):
                        pathlist.append((i,path%(i,e)))
    return pathlist

class disable_file_system_redirection:
    _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
    _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
    def __enter__(self):
        self.old_value = ctypes.c_long()
        self.success = self._disable(ctypes.byref(self.old_value))
    def __exit__(self, type, value, traceback):
        if self.success:
            self._revert(self.old_value)


with disable_file_system_redirection():
    browsers = {
        'Google': {"keys": "\Google\Chrome\ExtensionInstallBlacklist", "values": ["1",_winreg.REG_SZ,"*"]},
        'MsEdge': {"keys": "\Microsoft\Edge\ExtensionInstallBlocklist", "values" : ["1",_winreg.REG_SZ,"*"]},
        'mozilla': {"keys": "\Mozilla\Firefox\InstallAddonsPermission","values": ["Default",_winreg.REG_DWORD,0]}
        }
    for i in browsers:
        if re.findall(i,browserlist,re.IGNORECASE) and v==0:
            keys,value = browsers[i].values()
            try:
                creatingkey = _winreg.CreateKeyEx(_winreg.HKEY_LOCAL_MACHINE,"SOFTWARE\Policies\%s"%(keys),0,_winreg.KEY_ALL_ACCESS)
                _winreg.SetValueEx(creatingkey, value[0], 0, value[1], value[2])
            except Exception as err:
                print(err)
            else:
                print("successfully blocked the extensions in the %s browser"%(i))
        else:
            if re.findall(i,browserlist,re.IGNORECASE):
                keys,value = browsers[i].values()
                try:
                    _winreg.DeleteKeyEx(_winreg.HKEY_LOCAL_MACHINE,"SOFTWARE\Policies\%s"%(keys),_winreg.KEY_ALL_ACCESS,0)
                    print("successfully unblocked the extensions in the %s browser"%(i))
                except Exception as err:
                    print(err)
                
    users=os.popen("net users").read().split()[5:-4]
    fil_users=[i.strip() for i in users if i.strip()!="Administrator" and i.strip()!="Guest"]     
    if v==0:
        access = "deny"
    else:
        access = "grant"
    
    if re.findall("Opera",browserlist,re.IGNORECASE):
        path_creations=[(i,'"C:\Users\%s\AppData\Roaming\Opera Software\Opera Stable\Extensions"'%i) for i in fil_users if os.path.exists("C:\Users\%s\AppData\Roaming\Opera Software\Opera Stable\Extensions"%i)]
        for i in path_creations:
            cmd="icacls "+i[1]+" "+"/%s "%(access)+i[0]+":F /T"
            process=Popen(cmd,stdout=PIPE,stderr=PIPE)
            r,e=process.communicate()
            if e:
                print e
            else:
                print r
                if v==0:
                    print("successfully blocked the extensions for opera browser")
                else:
                    print("successfully unblocked the extensions for opera browser")
    if re.findall("Brave",browserlist,re.IGNORECASE):
        path_creations1=profilechecking("C:\Users\%s\AppData\Local\BraveSoftware\Brave-Browser\User Data","C:\Users\%s\AppData\Local\BraveSoftware\Brave-Browser\User Data\%s\Extensions")
        for i in path_creations1:
            cmd="icacls "+'"%s"'%(i[1])+" "+"/%s "%(access)+i[0]+":F /T"
            process=Popen(cmd,stdout=PIPE,stderr=PIPE)
            r,e=process.communicate()
            if e:
                print e
            else:
                print r
                if v==0:
                    print("successfully blocked the extensions for brave browser")
                else:
                    print("successfully unblocked the extensions for brave browser")