%META:TOPICINFO{author="ProjectContributor" date="1252721401" format="1.1" version="1"}%
%META:TOPICPARENT{name="UserDocumentationCategory"}%
%STARTINCLUDE%
---+ Access Control

_Restricting read and write access to topics and webs, by users and groups_

Access Control allows you restrict access to single topics and entire webs, by individual user and by user Groups. Access control, combined with UserAuthentication, lets you easily create and manage an extremely flexible, fine-grained privilege system.

%TOC%

#ImportantConsideration
---++ An important consideration

Open, freeform editing is the essence of WikiCulture - what makes Foswiki different and often more effective than other collaboration tools. For that reason, it is strongly recommended that the decision to restrict read or write access to a web or a topic is made with great care - the more restrictions, the less Wiki in the mix. Experience shows that _unrestricted write access_ works very well because:
   * *Peer influence* is enough to ensure that only relevant content is posted.
   * *Peer editing* - the ability for anyone to rearrange all content on a page - keeps topics focused.
   * In Foswiki, content is transparently preserved under *revision control*:
      * Edits can be undone by the administrator (per default a member of %USERSWEB%.AdminGroup; see [[#ManagingGroups]]).
      * Users are encouraged to edit and refactor (condense a long topic), since there's a safety net.
As a *collaboration guideline*, create broad-based Groups (for more and varied input), and avoid creating view-only Users (if you can read it, you should be able to contribute to it).

---++ Permissions settings of the webs on this Foswiki site
%INCLUDE{SitePermissions}%

<blockquote class="foswikiHelp">%H% Above table comes from SitePermissions</blockquote>

---++ Authentication vs. Access Control

*Authentication:* Identifies who a user is based on a login procedure. See UserAuthentication.

*Access control:* Restrict access to content based on users and groups once a user is identified.

---++ Users and groups

Access control is based on the familiar concept of users and groups. Users
are defined by their WikiNames. They can then be organized in unlimited
combinations by inclusion in one or more user Groups. Groups can also be
included in other Groups.

---+++ Managing Users

In standard Foswiki a user can create an account in UserRegistration. The following actions are performed:
   * WikiName and encrypted password are recorded using the password manager if authentication is enabled.
   * A confirmation e-mail is sent to the user.
   * A user home page with the WikiName of the user is created in the %USERSWEB% web.
   * The user is added to the %USERSWEB%.%WIKIUSERSTOPIC% topic.

The default visitor name is %USERSWEB%.WikiGuest. This is the non-authenticated user.

<blockquote class="foswikiHelp">%X% Your local Foswiki may have an alternate user mapping manager installed
which doesn't support user registration. Check with your Wiki administrator
if you are in doubt.</blockquote>

#ManagingGroups
---+++ Managing Groups

The following describes the standard Foswiki support for groups. Your local Foswiki may have an alternate group mapping manager installed. Check with your Wiki administrator if you are in doubt.

Groups are defined by group topics located in the ==%USERSWEB%== web. To create a new group, visit  %USERSWEB%.WikiGroups and enter the name of the new group ending in ==Group== into the "new group" form field. This will create a new group topic with two important settings:
   * ==Set <nop>GROUP = &lt; list of users and/or groups &gt;==
   * ==Set <nop>ALLOWTOPICCHANGE = &lt; list of users and/or groups &gt;==

The GROUP setting is a comma-separated list of users and/or other groups. Example:
   * ==Set GROUP = %USERSWEB%.<nop>SomeUser, %USERSWEB%.<nop>OtherUser, %USERSWEB%.<nop>SomeGroup==

The =ALLOWTOPICCHANGE= setting defines who is allowed to change the group topic; it is a comma delimited list of users and groups. You typically want to restrict that to the members of the group itself, so it should contain the name of the topic. This prevents users not in the group from editing the topic to give themselves or others access. For example, for the <nop>KasabianGroup topic write:
   * ==Set <nop>ALLOWTOPICCHANGE = %USERSWEB%.<nop>KasabianGroup==

<blockquote class="foswikiHelp">%X% Foswiki has strict formatting rules. Make sure you have three spaces, an asterisk, and an extra space in front of any access control rule.</blockquote>

#SuperAdminGroup
---+++ The Super Admin Group

A number of Foswiki functions (for example, renaming webs) are only available to administrators. Administrators are simply users who belong to the *SuperAdminGroup*. This is a standard user group, the name of which is defined by {SuperAdminGroup} setting in [[%SCRIPTURLPATH{"configure"}%][configure]]. The default name of this group is the =AdminGroup=. The system administrator may have chosen a different name for this group if your local Foswiki uses an alternate group mapping manager, but for simplicity we will use the default name !AdminGroup in the rest of this topic.

You can create new administrators simply by adding them to the %USERSWEB%.AdminGroup topic. For example,
   * ==Set <nop>GROUP = <nop>%USERSWEB%.ElizabethWindsor, <nop>%USERSWEB%.TonyBlair==
A member of the Super Admin Group has *unrestricted access* throughout the wiki, so only trusted staff should be added to this group.

---++ Restricting Access

Access to webs and topics is controlled by setting the values of certain
[[PreferenceSettings][preferences]]. These names of these preferences have the general form:

_permission_ _context_ _mode_

Where _permission is =ALLOW= or =DENY=, _context_ is =TOPIC=, =WEB=, or =ROOT=, and _mode_ is =VIEW=, =CHANGE=, or =RENAME=. For example, the preference =ALLOWTOPICCHANGE= lists who is allowed to change
topics in the current web.

   * Restricting VIEW blocks viewing and searching of content. When you restric VIEW to a topic or web, this also restricts [[VarINCLUDE][INCLUDE]] and [[FormattedSearch][Formatted SEARCH]] from showing the content of the topics.
   * Restricting CHANGE blocks creating new topics, changing topics or attaching files.
   * Restricting RENAME prevents renaming of topics within a web.
   
<blockquote class="foswikiHelp">%X% *There is an important distinction between CHANGE access and RENAME access.* A user can CHANGE a topic, but thanks to version control their changes cannot be lost (the history of the topic before the change is recorded). However if a topic or web is renamed, that history may be lost. Typically a site will only give RENAME access to administrators and content owners.

%X% Be warned that some plugins may not respect access permissions.

%I% [[Macros#FinalMacros][FINALPREFERENCES]] affects access controls, allowing you to prevent changes to access control settings while still allowing edit access to topics.</blockquote>

---+++ Controlling default access to the entire Wiki.
There are a number of external to Foswiki ways to require authentication before viewing any part of the wiki (apache rewrites, apache auth, proxying, etc).
Or, you can use =configure= to set the *EXPERT* setting ={AuthScripts}= to contain _all_ the scripts in the =foswiki/bin= directory.

However, it can be desirable to default all your created webs to exclude %USERSWEB%.WikiGuest by default, and then to open up only some topics, or some webs for guest users.

This can be acheived by setting the =DENYWEBVIEW= setting (as below) in the %LOCALSITEPREFS% topic, and then 'un-setting' it for specific Web's or topics.

__Note:__ that your %USERSWEB%, %SYSTEMWEB% and Sandbox webs are still viewable by guest users

---+++ Controlling access to a Web

You can define restrictions on who is allowed to view a <nop>%WIKITOOLNAME% web. You can restrict access to certain webs to selected users and groups, by:

   * *authenticating all webs and restricting selected webs:* Topic access in all webs is authenticated, and selected webs have restricted access.
   * *authenticating and restricting selected webs only:* Provide unrestricted viewing access to open webs, with authentication and restriction only on selected webs.

   * You can define these settings in the %WEBPREFSTOPIC% topic, preferable towards the end of the topic:
      * ==Set <nop>DENYWEBVIEW = &lt; comma-delimited list of users and groups &gt;==
      * ==Set <nop>ALLOWWEBVIEW = &lt; comma-delimited list of users and groups &gt;==
      * ==Set <nop>DENYWEBCHANGE = &lt; comma-delimited list of users and groups &gt;==
      * ==Set <nop>ALLOWWEBCHANGE = &lt; comma-delimited list of users and groups &gt;==
      * ==Set <nop>DENYWEBRENAME = &lt; comma-delimited list of users and groups &gt;==
      * ==Set <nop>ALLOWWEBRENAME = &lt; comma-delimited list of users and groups &gt;==

If your site allows hierarchical webs, then access to sub-webs is determined from the access controls of the parent web, plus the access controls in the sub-web. So, if the parent web has ==ALLOWWEBVIEW== set, this will also apply to the subweb. Also note that you will need to ensure that the parent web's ==FINALPREFERENCES== does not include the access control settings listed above. Otherwise you will not be able override the parent web's access control settings in sub-webs.

Creation and renaming of sub-webs is controlled by the WEBCHANGE setting on the parent web (or ROOTCHANGE for [[#RootWebs][root webs]]). Renaming is additionally restricted by the setting of WEBRENAME in the web itself.

---+++ Controlling access to a topic

   * You can define these settings in any topic, preferable towards the end of the topic:
      * ==Set <nop>DENYTOPICVIEW = &lt; comma-delimited list of users and groups &gt;==
      * ==Set <nop>ALLOWTOPICVIEW = &lt; comma-delimited list of users and groups &gt;==
      * ==Set <nop>DENYTOPICCHANGE = &lt; comma-delimited list of users and groups &gt;==
      * ==Set <nop>ALLOWTOPICCHANGE = &lt; comma-delimited list of users and groups &gt;==
      * ==Set <nop>DENYTOPICRENAME = &lt; comma-delimited list of users and groups &gt;==
      * ==Set <nop>ALLOWTOPICRENAME = &lt; comma-delimited list of users and groups &gt;==

<blockquote class="foswikiHelp">%X% *Caution!* Settings are always taken from the latest (current) revision of a topic.  If older revisions of a topic had more restrictive access controls, they will not be used when accessing the older revision.</blockquote>

Remember when opening up access to specific topics within a restricted web that other topics in the web - for example, the !WebLeftBar - may also be accessed when viewing the topics. The message you get when you are denied access should tell you what topic you were not permitted to access.

*Be careful* with empty values for any of these.

   * ==Set <nop>ALLOWTOPICVIEW = <nop>== <br />This means the same as not setting it at all. (This was documented wrong in versions 4.0.X, 4.1.0 and 4.1.1)

   * ==Set <nop>DENYTOPICVIEW = <nop>== <br />This means _do not deny anyone the right to view this topic_. If DENYTOPICVIEW is set to an empty value anyone has access even if ALLOWTOPICVIEW or ALLOWWEBVIEW is defined. This allows you to have very restrictive default access rights to an entire web and still allow individual topics to have more open access.

The same rules apply to ALLOWTOPICCHANGE/DENYTOPICCHANGE and APPLYTOPICRENAME/DENYTOPICRENAME. Setting ALLOWTOPICCHANGE or ALLOWTOPICRENAME to en empty value means the same as not defining it. Setting DENYTOPICCHANGE or DENYTOPICRENAME to an empty value means that anyone can edit or rename the topic.

<blockquote class="foswikiHelp">%X% If the same setting is defined multiple times the last one overrides the previous. They are not OR'ed together.

%X% _Setting to an empty value has caused confusion and great debate and it has been decided that the empty setting syntax will be replaced by something which is easier to understand in a later version of Foswiki. A method to upgrade will be provided. Please read the release notes carefully when you upgrade._ </blockquote>

See "How Foswiki evaluates ALLOW/DENY settings" below for more on how ALLOW and DENY interacts.

---+++ Controlling access to attachments

Attachments are referred to directly, and are not normally indirected via Foswiki scripts. This means that the above instructions for access control will _not_ apply to attachments. It is possible that someone may inadvertently publicise a URL that they expected to be access-controlled.

The easiest way to apply the same access control rules for attachments as apply to topics is to use the Apache =mod_rewrite= module, and configure your webserver to redirect accesses to attachments to the Foswiki =viewfile= script. For example,

<verbatim>
    ScriptAlias /foswiki/bin/ /filesystem/path/to/bin/
    Alias /foswiki/pub/       /filesystem/path/to/pub/

    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^/+foswiki/+pub/+System/+.+
    RewriteRule ^/+foswiki/+pub/+([^/]+)((/+([^/]+))+)/+(.+) /foswiki/bin/viewfile/$1/$2?filename=$5 [L,PT]
</verbatim>

That way all the controls that apply to the topic also apply to attachments to the topic. Other types of webserver have similar support.

<blockquote class="foswikiHelp">%X% Images embedded in topics will load much slower since each image will be delivered by the =viewfile= script.</blockquote>

#RootWebs
---+++ Controlling who can manage top-level webs

Top level webs are a special case, because they don't have a parent web with a %WEBPREFSTOPIC%. So there has to be a special control just for the root level.
   * You can define these settings in the %LOCALSITEPREFS% topic, preferably towards the end of the topic:
      * ==Set <nop>DENYROOTCHANGE = &lt; comma-delimited list of users and groups &gt;==
      * ==Set <nop>ALLOWROOTCHANGE = &lt; comma-delimited list of users and groups &gt;==
Note that you do *not* require =ROOTCHANGE= access to rename an existing top-level web. You just need =WEBCHANGE= in the web itself.

---+++ How Foswiki evaluates ALLOW/DENY settings

When deciding whether to grant access, Foswiki evaluates the following rules in order (read from the top of the list; if the logic arrives at *PERMITTED* or *DENIED* that applies immediately and no more rules are applied). You need to read the rules bearing in mind that VIEW, CHANGE and RENAME access may be granted/denied separately.

<blockquote class="foswikiHelp">%X% Settings are only read from the most current (latest) revision of a topic.  Settings from older revisions are never used, even when viewing an older revision with the =rdiff= script</blockquote>

   1 If the user is an [[#SuperAdminGroup][administrator]]
      * access is *PERMITTED*.
   1 If DENYTOPIC is set to a list of wikinames
      * people in the list will be *DENIED*.
   1 If DENYTOPIC is set to _empty_ ( i.e. <tt>Set DENYTOPIC =</tt> )
      * access is *PERMITTED* _i.e_ no-one is denied access to this topic.%BR%
        <blockquote class="foswikiHelp">%X% *Attention:* Use this with caution. This is *deprecated* and will likely change in the next release.</blockquote>
   1 If ALLOWTOPIC is set
      1 people in the list are *PERMITTED*
      1 everyone else is *DENIED*
   1 If DENYWEB is set to a list of wikinames
      * people in the list are *DENIED* access
   1 If ALLOWWEB is set to a list of wikinames
      * people in the list will be *PERMITTED*
      * everyone else will be *DENIED*
   1 If you got this far, access is *PERMITTED*
   
---++ Access control and INCLUDE

ALLOWTOPICVIEW and ALLOWTOPICCHANGE only applies to the topic in which the settings are defined. If a topic A includes another topic B, topic A does not inherit the access rights of the included topic B.

Examples: Topic A includes topic B

   * If the included topic B has ALLOWTOPICCHANGE set to block editing for a user, it does not prevent editing the including topic A.
   * If the included topic B has ALLOWTOPICVIEW set to block view for a user, the user can still view topic A but he cannot see the included topic B. He will see a message _No permission to view B_ 

---++ Access control quick recipes

---+++ Obfuscating webs

Another way of hiding webs is to keep them hidden by not publishing the URL and by preventing the ==all webs== search option from accessing obfuscated webs. Do so by setting the ==NOSEARCHALL== setting in %WEBPREFSTOPIC%:
   * ==Set <nop>NOSEARCHALL = on== 

<blockquote class="foswikiHelp">%T%  Setting <nop>NOSEARCHALL to __any__ value other than the empty string will hide a web.  Setting ==<nop>NOSEARCHALL = off== will have the same effect as setting it to ==on== </blockquote>

This setup can be useful to hide a new web until content its ready for deployment, or to hide view access restricted webs.

<blockquote class="foswikiHelp">%X% Obfuscating a web without view access control is *very* insecure, as anyone who knows the URL can access the web.</blockquote>

---+++ Restrict Access to a whole Foswiki site

For a firewalled Foswiki, e.g. an intranet wiki or extranet wiki, you want to allow only invited people to access your Foswiki. In this case, enable [[UserAuthentication#ApacheLogin][user authentication with ApacheLogin]] and lock down access to the whole =bin= and =pub= directories to all but valid users. In the Apache =.htaccess= file or the appropriate =.conf= file, replace the =&lt;FilesMatch "(attach|edit|...= section with this:

<verbatim>
<FilesMatch ".*">
       require valid-user
</FilesMatch>
</verbatim>

If needed, you can further restrict access to selected webs with ALLOWWEBVIEW and other access control settings.

<blockquote class="foswikiHelp">%H% With this configuration, someone with access to the site needs to register new users.</blockquote>

---+++ Authenticate all webs and restrict selected webs

Use the following setup to authenticate users for topic viewing in all webs and to restrict access to selected webs. Requires UserAuthentication to be enabled.

   1 The simple way is to add this to =%WEBPREFSTOPIC%= in all webs.
      * ==Set <nop>DENYWEBVIEW = !WikiGuest==
   1. *Restrict* view access to selected users and groups. Set one or both of these settings in its %WEBPREFSTOPIC% topic:
      * ==Set <nop>ALLOWWEBVIEW = &lt; list of users and groups &gt;==
      * *Note:* =DENYWEBVIEW= is evaluated before =ALLOWWEBVIEW=. Access is denied if the authenticated person is in the =DENYWEBVIEW= list, or not in the =ALLOWWEBVIEW= list. Access is granted if =DENYWEBVIEW= and =ALLOWWEBVIEW= are not defined.

In rare cases it may be required to authenticate the view script. This can in some cases have a dramatic performance hit because the webserver must re-authenticate for every page view.

   1. Set =require valid-user= on your =view= script in .htaccess or the appropriate Apache .conf file. This looks like: =FilesMatch "(attach|edit|manage|rename|save|view|upload|mail|logon|.*auth).*"= (normally =view= is not in that list).

---+++ Authenticate and restrict selected webs only

Use the following setup to provide unrestricted viewing access to open webs, with authentication only on selected webs. Requires UserAuthentication to be enabled.

   1. *Restrict* view access to selected users and groups. Set one or both of these settings in its %WEBPREFSTOPIC% topic:
      * ==Set <nop>DENYWEBVIEW = &lt; list of users and groups &gt;==
      * ==Set <nop>ALLOWWEBVIEW = &lt; list of users and groups &gt;==
      * *Note:* =DENYWEBVIEW= is evaluated before =ALLOWWEBVIEW=. Access is denied if the authenticated person is in the =DENYWEBVIEW= list, or not in the =ALLOWWEBVIEW= list. Access is granted if =DENYWEBVIEW= and =ALLOWWEBVIEW= are not defined.

---+++ Show control settings
You can list the access controls affecting a topic using the [[VarSHOWPREFERENCE][%<nop>SHOWPREFERENCE{}%]] macro in the topic, thus:
<verbatim class="tml">
%SHOWPREFERENCE{"DENYWEBVIEW,ALLOWWEBVIEW,DENYWEBCHANGE,ALLOWWEBCHANGE,DENYWEBRENAME,ALLOWWEBRENAME"}%
</verbatim>
For this topic, this displays:
%SHOWPREFERENCE{"DENYWEBVIEW,ALLOWWEBVIEW,DENYWEBCHANGE,ALLOWWEBCHANGE,DENYWEBRENAME,ALLOWWEBRENAME"}%

---+++ Hide control settings

To hide access control settings from normal browser viewing, you can put them into the _topic [[%SYSTEMWEB%.PreferenceSettings][preference settings]]_ by clicking the link =Edit topic preference settings= under =More topic actions= menu. Preferences set in this manner are not visible in the topic text, but take effect nevertheless. Access control settings added as topic preference settings are stored in the topic meta data and they override settings defined in the topic text.

Alternatively, place them in HTML comment markers, but this exposes the access setting during ordinary editing.

<pre class="tml">
==&lt;!--== <br />
==&nbsp;  * Set <nop>DENYTOPICCHANGE = %USERSWEB%.<nop>SomeGroup== <br />
==--&gt;==
</pre>

%STOPINCLUDE%

---
*Related Topics:* AdminDocumentationCategory, UserAuthentication
<!-- %JQREQUIRE{"chili"}% -->
