title: Alfresco : Non-Administrator users cannot turn a site into moderate mode
body: <p>I've got an issue on the Share part of my Alfresco application.
  Indeed, when I'm logged in under a normal account (not administrator) and I go to a created site that dont have his "Moderate" option checked, I've got an error message each time I want to modify the site's details and check the moderate option.<br/>
  The error message is the following :</p>
  
  <pre><code>04110011 Wrapped Exception (with status template): 04110169 Failed to execute script 'classpath*:alfresco/templates/webscripts/org/alfresco/repository/site/site.put.json.js': 04110168 Access denied. You don't have the permission to perform this operation.
  </code></pre>
  
  <p>The problem never occurs when the site has already been created under the "moderate" option or when your logged in under the administrator account.</p>
  
  <p>Here you have the console log :</p>
  
  <pre><code>ERROR [extensions.webscripts.AbstractRuntime] Exception from executeScript - redirecting to status template error: 04110001 Wrapped Exception (with status template): 04110097 Failed to execute script 'classpath*:alfresco/templates/webscripts/org/alfresco/repository/site/site.put.json.js': 04110096 Access refusé.  Vous n'avez pas la permission de réaliser cette opération.
  org.springframework.extensions.webscripts.WebScriptException: 04110001 Wrapped Exception (with status template): 04110097 Failed to execute script 'classpath*:alfresco/templates/webscripts/org/alfresco/repository/site/site.put.json.js': 04110096 Access refusé.  Vous n'avez pas la permission de réaliser cette opération.
      at org.springframework.extensions.webscripts.AbstractWebScript.createStatusException(AbstractWebScript.java:758)
      at org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:171)
      at org.alfresco.repo.web.scripts.RepositoryContainer$2.execute(RepositoryContainer.java:383)
      at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:381)
      at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:436)
      at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:466)
      at org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:304)
      at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:333)
      at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:189)
      at org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:118)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.alfresco.web.app.servlet.GlobalLocalizationFilter.doFilter(GlobalLocalizationFilter.java:58)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
      at java.lang.Thread.run(Unknown Source)
  Caused by: org.alfresco.scripts.ScriptException: 04110097 Failed to execute script 'classpath*:alfresco/templates/webscripts/org/alfresco/repository/site/site.put.json.js': 04110096 Access refusé.  Vous n'avez pas la permission de réaliser cette opération.
      at org.alfresco.repo.jscript.RhinoScriptProcessor.execute(RhinoScriptProcessor.java:194)
      at org.alfresco.repo.processor.ScriptServiceImpl.executeScript(ScriptServiceImpl.java:282)
      at org.alfresco.repo.web.scripts.RepositoryScriptProcessor.executeScript(RepositoryScriptProcessor.java:102)
      at org.springframework.extensions.webscripts.AbstractWebScript.executeScript(AbstractWebScript.java:981)
      at org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:86)
      ... 24 more
  Caused by: org.alfresco.repo.security.permissions.AccessDeniedException: 04110096 Access refusé.  Vous n'avez pas la permission de réaliser cette opération.
      at org.alfresco.repo.security.permissions.impl.ExceptionTranslatorMethodInterceptor.invoke(ExceptionTranslatorMethodInterceptor.java:48)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.alfresco.repo.audit.AuditMethodInterceptor.proceedWithAudit(AuditMethodInterceptor.java:217)
      at org.alfresco.repo.audit.AuditMethodInterceptor.proceed(AuditMethodInterceptor.java:184)
      at org.alfresco.repo.audit.AuditMethodInterceptor.invoke(AuditMethodInterceptor.java:137)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
      at $Proxy53.setPermission(Unknown Source)
      at org.alfresco.repo.site.SiteServiceImpl.setModeratedPermissions(SiteServiceImpl.java:1680)
      at org.alfresco.repo.site.SiteServiceImpl.updateSite(SiteServiceImpl.java:1068)
      at org.alfresco.repo.site.script.Site.save(Site.java:279)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:155)
      at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:243)
      at org.mozilla.javascript.optimizer.OptRuntime.callProp0(OptRuntime.java:119)
      at org.mozilla.javascript.gen.c44._c11(file:/C:/workspace_starter/Tomcat/home/tomcat/webapps/alfresco/WEB-INF/classes/alfresco/templates/webscripts/org/alfresco/repository/site/site.put.json.js:337)
      at org.mozilla.javascript.gen.c44.call(file:/C:/workspace_starter/Tomcat/home/tomcat/webapps/alfresco/WEB-INF/classes/alfresco/templates/webscripts/org/alfresco/repository/site/site.put.json.js)
      at org.mozilla.javascript.optimizer.OptRuntime.callName0(OptRuntime.java:108)
      at org.mozilla.javascript.gen.c44._c0(file:/C:/workspace_starter/Tomcat/home/tomcat/webapps/alfresco/WEB-INF/classes/alfresco/templates/webscripts/org/alfresco/repository/site/site.put.json.js:366)
      at org.mozilla.javascript.gen.c44.call(file:/C:/workspace_starter/Tomcat/home/tomcat/webapps/alfresco/WEB-INF/classes/alfresco/templates/webscripts/org/alfresco/repository/site/site.put.json.js)
      at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:393)
      at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2834)
      at org.mozilla.javascript.gen.c44.call(file:/C:/workspace_starter/Tomcat/home/tomcat/webapps/alfresco/WEB-INF/classes/alfresco/templates/webscripts/org/alfresco/repository/site/site.put.json.js)
      at org.mozilla.javascript.gen.c44.exec(file:/C:/workspace_starter/Tomcat/home/tomcat/webapps/alfresco/WEB-INF/classes/alfresco/templates/webscripts/org/alfresco/repository/site/site.put.json.js)
      at org.alfresco.repo.jscript.RhinoScriptProcessor.executeScriptImpl(RhinoScriptProcessor.java:472)
      at org.alfresco.repo.jscript.RhinoScriptProcessor.execute(RhinoScriptProcessor.java:190)
      ... 28 more
  Caused by: net.sf.acegisecurity.AccessDeniedException: Access is denied.
      at net.sf.acegisecurity.vote.AffirmativeBased.decide(AffirmativeBased.java:86)
      at net.sf.acegisecurity.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:394)
      at net.sf.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:77)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.alfresco.repo.security.permissions.impl.ExceptionTranslatorMethodInterceptor.invoke(ExceptionTranslatorMethodInterceptor.java:44)
      ... 58 more
  </code></pre>
  
  <p>The error come from the "site.put.json.js" webscript, that contains the following source code :</p>
  
  <pre><code>&lt;import resource="classpath:alfresco/module/customAlfrescoAMP/javascript/tsUtils.js"&gt;
  &lt;import resource="classpath:alfresco/module/customAlfrescoAMP/javascript/ts-constants.js"&gt;
  
  function main()
  {
      // Get the site
      var shortName = url.extension;
      var site = siteService.getSite(shortName);
  
      if (site != null)
      {   
          // Updafte the sites details
          if (json.has("title") == true)
          {
             site.title = json.get("title");
          }
          if (json.has("description") == true)
          {
             site.description = json.get("description");
          }
  
          // Use the visibility flag before the isPublic flag
          if (json.has("visibility") == true)
          {
             site.visibility = json.get("visibility");
          }
          else if (json.has("isPublic") == true)
          {
             // Deal with deprecated isPublic flag accordingly
             var isPublic = json.getBoolean("isPublic");
             if (isPublic == true)
             {
                site.visibility = siteService.PUBLIC_SITE;
             }
             else
             {
                site.visibility = siteService.PRIVATE_SITE;
             }
          }
  
          //début modif T&amp;S
          if (!site.node.hasAspect("st:siteType"))
          {
              site.node.addAspect("st:siteType");
          }
  
          var siteType = "";
          if (json.has("type") == true)
          {
              siteType = json.get("type");
          }
  
          var assocs = site.node.assocs["st:siteTypeAssoc"];
          if (assocs != null &amp;&amp; assocs.length == 1) {
              site.node.removeAssociation(assocs[0], "st:siteTypeAssoc");
              assocs[0].remove();
          }
          var siteTypeNode = site.node.createNode(null, "cm:content");
          siteTypeNode.content = siteType;
          site.node.createAssociation(siteTypeNode, "st:siteTypeAssoc");
  
          var siteiconNodeRef = null;
          if (json.has("siteiconNodeRef"))
          {
              siteiconNodeRef = json.get("siteiconNodeRef");
          }
  
          if (siteiconNodeRef != null &amp;&amp; siteiconNodeRef != "")
          {
             var image = search.findNode(siteiconNodeRef);
  
             // ensure cm:person has 'cm:preferences' aspect applied - as we want to add the avatar as
             // the child node of the 'cm:preferenceImage' association
             if (!site.node.hasAspect("st:siteIcon"))
             {
                 site.node.addAspect("st:siteIcon");
             }
  
             // remove old image child node if we already have one
             var assocs = site.node.assocs["st:siteIconAssoc"];
             if (assocs != null &amp;&amp; assocs.length == 1)
             {
                 site.node.removeAssociation(assocs[0], "st:siteIconAssoc");
                 //no need to remove the image ?
                 //assocs[0].remove();
             }
  
             //images are created in userhome, move them to the site
             image.move(site.node);
  
             site.node.createAssociation(image, "st:siteIconAssoc");
          }
          //fin modif T&amp;S
  
          // Save the site
          site.save();
  
          var members = site.listMembers(null, null, 0);
  
          var templateNodeRef = search.luceneSearch( "@\\{http\\://www.alfresco.org/model/content/1.0\\}name:\"edit-site-mail.ftl\"")[0].nodeRef;
  
          for (username in members)
          {
          /*var mail = actions.create("mail");
          mail.parameters.to_many = users;
          mail.parameters.subject = EDIT_SITE_MAIL_SUBJECT + site.title;
          mail.parameters.template = templateNodeRef;
          mail.execute(site.node);
          */
  
          createAndSendHtmlMail(site.node, EDIT_SITE_MAIL_SUBJECT + site.title, people.getPerson(username).properties["cm:email"], "", templateNodeRef, null);
          }
  
          // Pass the model to the template
          model.site = site;
      }
      else
      {
          // Return 404
          status.setCode(status.STATUS_NOT_FOUND, "Site " + shortName + " does not exist");
          return;
      }
  }
  
  main();
  </code></pre>
  
  <p>So, if you have any suggestion...</p>