<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
	<id>http://wiki.mipt.ru/index.php?action=history&amp;feed=atom&amp;title=Development%3ANEWMODULE_Adding_capabilities</id>
	<title>Development:NEWMODULE Adding capabilities - История изменений</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.mipt.ru/index.php?action=history&amp;feed=atom&amp;title=Development%3ANEWMODULE_Adding_capabilities"/>
	<link rel="alternate" type="text/html" href="http://wiki.mipt.ru/index.php?title=Development:NEWMODULE_Adding_capabilities&amp;action=history"/>
	<updated>2026-05-07T15:53:41Z</updated>
	<subtitle>История изменений этой страницы в вики</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>http://wiki.mipt.ru/index.php?title=Development:NEWMODULE_Adding_capabilities&amp;diff=11450&amp;oldid=prev</id>
		<title>Олег Давидович: 1 версия импортирована</title>
		<link rel="alternate" type="text/html" href="http://wiki.mipt.ru/index.php?title=Development:NEWMODULE_Adding_capabilities&amp;diff=11450&amp;oldid=prev"/>
		<updated>2024-10-21T08:51:34Z</updated>

		<summary type="html">&lt;p&gt;1 версия импортирована&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Предыдущая версия&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Версия от 08:51, 21 октября 2024&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Олег Давидович</name></author>
	</entry>
	<entry>
		<id>http://wiki.mipt.ru/index.php?title=Development:NEWMODULE_Adding_capabilities&amp;diff=11449&amp;oldid=prev</id>
		<title>1&gt;Nakohdo: /* contextlevel */  linking to Context</title>
		<link rel="alternate" type="text/html" href="http://wiki.mipt.ru/index.php?title=Development:NEWMODULE_Adding_capabilities&amp;diff=11449&amp;oldid=prev"/>
		<updated>2010-11-09T14:54:07Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;contextlevel: &lt;/span&gt;  linking to &lt;a href=&quot;/index.php?title=Context&quot; class=&quot;mw-redirect&quot; title=&quot;Context&quot;&gt;Context&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{New_Module}}&lt;br /&gt;
In order to add a capabilities for your &amp;lt;&amp;lt;NEWMODULE&amp;gt;&amp;gt; you need to:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==1. Create a file access.php in the &amp;lt;&amp;lt;NEWMODULE&amp;gt;&amp;gt;/db directory==&lt;br /&gt;
&lt;br /&gt;
This should contain a list of the capabilities that you want to define. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$mod_&amp;lt;&amp;lt;NEWMODULE&amp;gt;&amp;gt;_capabilities = array(&lt;br /&gt;
    &amp;#039;mod/&amp;lt;&amp;lt;NEWMODULE&amp;gt;&amp;gt;:&amp;lt;&amp;lt;CAPABILITYNAME&amp;gt;&amp;gt;&amp;#039; =&amp;gt; array(&lt;br /&gt;
        &amp;#039;riskbitmask&amp;#039; =&amp;gt; RISK_SPAM | RISK_PERSONAL | RISK_XSS | RISK_CONFIG,&lt;br /&gt;
        &amp;#039;captype&amp;#039; =&amp;gt; &amp;#039;write&amp;#039;,&lt;br /&gt;
        &amp;#039;contextlevel&amp;#039; =&amp;gt; CONTEXT_MODULE,&lt;br /&gt;
        &amp;#039;legacy&amp;#039; =&amp;gt; array(&lt;br /&gt;
            &amp;#039;student&amp;#039; =&amp;gt; CAP_ALLOW,&lt;br /&gt;
            &amp;#039;teacher&amp;#039; =&amp;gt; CAP_ALLOW,&lt;br /&gt;
            &amp;#039;editingteacher&amp;#039; =&amp;gt; CAP_ALLOW,&lt;br /&gt;
            &amp;#039;admin&amp;#039; =&amp;gt; CAP_ALLOW&lt;br /&gt;
        )&lt;br /&gt;
    ),&lt;br /&gt;
&lt;br /&gt;
    // Add more capabilities here ...&lt;br /&gt;
)&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The various parts of the capability definition are:&lt;br /&gt;
&lt;br /&gt;
===Capability name (&amp;#039;mod/&amp;lt;&amp;lt;NEWMODULE&amp;gt;&amp;gt;:&amp;lt;&amp;lt;CAPABILITYNAME&amp;gt;&amp;gt;)===&lt;br /&gt;
&lt;br /&gt;
This is the internal name used for this this capability. In addition to this internal name, you should also add the language string &amp;lt;&amp;lt;NEWMODULE&amp;gt;&amp;gt;:&amp;lt;&amp;lt;CAPABILITYNAME&amp;gt;&amp;gt; to your module&amp;#039;s language file, to give the capability a name that users will see in the interface.&lt;br /&gt;
&lt;br /&gt;
===riskbitmask===&lt;br /&gt;
&lt;br /&gt;
Allowing people to do various things sometimes requires introducing possible security risks. For example, if you can post to a forum, you can post unsolicited advertising. To a certain extent users have to be trusted. To help administrators and teachers know what the issues are, each capability should list any associated risks. See [[Development:Hardening_new_Roles_system]]. will be reflected in the list of icons of each row of the &amp;#039;Override permissions&amp;#039;-&amp;gt;roles page.&lt;br /&gt;
&lt;br /&gt;
Technically, this value is a bit field, so you should combine the relevant risks constants with the &amp;#039;|&amp;#039; operator. So typical values might be:&lt;br /&gt;
* RISK_SPAM&lt;br /&gt;
* RISK_PERSONAL | RISK_XSS | RISK_DATALOSS&lt;br /&gt;
&lt;br /&gt;
===captype===&lt;br /&gt;
&lt;br /&gt;
Should be either &amp;#039;read&amp;#039; or &amp;#039;write&amp;#039;. &amp;#039;read&amp;#039; is for capabilities that just let you view things. &amp;#039;write&amp;#039; for capabilities that let you change things.&lt;br /&gt;
&lt;br /&gt;
===contextlevel===&lt;br /&gt;
&lt;br /&gt;
The context level where this capability is most relevant. If you are writing a module this will almost always be [[Context|CONTEXT_MODULE]]. (This does not have much effect. It is just used to sort and group capabilities on the define roles and override roles pages.)&lt;br /&gt;
&lt;br /&gt;
===legacy===&lt;br /&gt;
&lt;br /&gt;
This is badly named, it would be better called &amp;#039;defaultpermissions&amp;#039; or something.&lt;br /&gt;
&lt;br /&gt;
This section defines, for each legacy role type, what permissions those roles should be given when your module is first installed (or when a new capability is detected on upgrade).&lt;br /&gt;
&lt;br /&gt;
Normally, you just add one line for each role that you want to give the capability to. The line should look like &amp;#039;roletype&amp;#039; =&amp;gt; CAP_ALLOW. Just leave out roles that you do not want to get the capability be default. Very exceptionally, you may need to specify a default permission of CAP_PREVENT, CAP_PROHIBIT.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==2. Get Moodle to load the updated capabilities==&lt;br /&gt;
&lt;br /&gt;
The capabilities you defined are only read (and copied into the Moodle database) when your module is installed or upgraded. So every time you edit the db/access.php file you must&lt;br /&gt;
# Increase your module&amp;#039;s version number by editing the file mod/&amp;lt;&amp;lt;NEWMODULE&amp;gt;&amp;gt;/version.php.&lt;br /&gt;
# Go to the the Administration ► Notifications page, and click through the steps to let Moodle upgrade itself. You should see the name of your module in one of the steps.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==3. Checking the capability in your code==&lt;br /&gt;
&lt;br /&gt;
In order to check whether the current user has a particular capability, you need to use the has_capability function. To do that, first you have to get the appropriate context. In this case, it will be a module context.&lt;br /&gt;
&lt;br /&gt;
1. First we need to get the $cm id, and verify that it is correct (there are lots of different ways you might do this, this is only an example.&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$cmid = required_param(&amp;#039;cmid&amp;#039;, PARAM_INT);&lt;br /&gt;
if (!$cm = get_coursemodule_from_id(&amp;#039;&amp;lt;&amp;lt;NEWMODULE&amp;gt;&amp;gt;&amp;#039;, $cmid)) {&lt;br /&gt;
    error(&amp;quot;Course module ID was incorrect&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
2. Then you get the module context:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$context = get_context_instance(CONTEXT_MODULE, $cm-&amp;gt;id);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
3. Finally, you can actually check the permission&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
if (has_capability(&amp;#039;mod/&amp;lt;&amp;lt;NEWMODULE&amp;gt;&amp;gt;:&amp;lt;&amp;lt;CAPABILITYNAME&amp;gt;&amp;gt;&amp;#039;, $context)) {&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Normally, you do 1. and 2. once at the top of a script, and then call has_capability as needed within the script with the appropriate capabilities.&lt;br /&gt;
&lt;br /&gt;
===Useful variations===&lt;br /&gt;
&lt;br /&gt;
====Controlling overall access to a script====&lt;br /&gt;
&lt;br /&gt;
Suppose you have a page that should only be available to users with a particular capability. For example, only users with mod/quiz:viewreports should be able to access mod/quiz/report.php. In cases like this, you can use the require_capability function:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
require_capability($capability, $context);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
near the top of your script. (As soon as you have got the context and called require_login is a good time.) All this does internally is&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
if (!has_capability($capability, $context)) {&lt;br /&gt;
    // Display error and exit.&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
but using require_capability makes your code simpler and is recommended. (Of course, anywhere you might print a link to a page like this, you should only print the link if the user has the right capability.)&lt;br /&gt;
&lt;br /&gt;
====Getting a list of users with a capability====&lt;br /&gt;
&lt;br /&gt;
Suppose you need to get a list of all the users with a particular capability. (For example, the quiz reports list all the users with the mod/quiz:attempt capability. Then you can use the get_users_by_capability function. &lt;br /&gt;
&lt;br /&gt;
====Checking the permissions of another user====&lt;br /&gt;
&lt;br /&gt;
There is an optional 3rd parameter to has_capability that you can use to check another user&amp;#039;s permissions:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
has_capability($capability, $context, $otheruser-&amp;gt;id);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Excluding administrators====&lt;br /&gt;
&lt;br /&gt;
Administrators have a magic &amp;#039;moodle/site:doanything&amp;#039; capability that gives them every other capability. If you wish to disable that magic override for one particular capability check, you can use the optional 4th parameter to has capability:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
has_capability($capability, $context, NULL, false);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
However, you normally should not do this.&lt;br /&gt;
&lt;br /&gt;
====Performance considerations====&lt;br /&gt;
&lt;br /&gt;
The has_capability function has been carefully optimised, and is pretty fast and you should not really worry. However, it has to perform a fairly complex computation, and if you are going to make exactly the same has_capability call several times in a page (perhaps in a loop) it is probably worth moving the permission check outside the loop. For example don&amp;#039;t do:&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
foreach ($attempts as $attempt) {&lt;br /&gt;
    if (has_capability(&amp;#039;mod/quiz:viewreports&amp;#039;, $context)) {&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Instead do&lt;br /&gt;
&amp;lt;code php&amp;gt;&lt;br /&gt;
$canviewreports = has_capability(&amp;#039;mod/quiz:viewreports&amp;#039;, $context);&lt;br /&gt;
foreach ($attempts as $attempt) {&lt;br /&gt;
    if ($canviewreports) {&lt;br /&gt;
        // ...&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
get_users_by_capability is a very expensive computation. If you are calling it more than once in your script, you are probably doing something wrong ;-)&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
&lt;br /&gt;
* [[Development:Roles]]&lt;br /&gt;
* [[Development:Hardening_new_Roles_system]] - information about risks&lt;br /&gt;
* [[Development:NEWMODULE_Documentation]] - NEWMODULE Documentation front page&lt;br /&gt;
&lt;br /&gt;
{{CategoryDeveloper}}&lt;br /&gt;
[[Category:Roles]]&lt;/div&gt;</summary>
		<author><name>1&gt;Nakohdo</name></author>
	</entry>
</feed>