Created
February 6, 2016 08:24
-
-
Save gh640/a967949e770c23918a36 to your computer and use it in GitHub Desktop.
Drupal 7 でユーザがロールを持つかどうかを調べる関数
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * ユーザが特定のロールを持つかどうかを調べる | |
| * | |
| * @param object $account | |
| * チェック対象ユーザのオブジェクト。 | |
| * @param array|int $roles | |
| * ロールの文字列の配列。インデックス配列。 | |
| * | |
| * @return bool | |
| * 指定されたロールのうち少なくとも 1 つのロールを | |
| * ユーザが持っていれば TRUE その他は FALSE 。 | |
| */ | |
| function _mymodule_user_has_role($account, $roles) { | |
| if (!is_array($roles)) { | |
| $roles = array($roles); | |
| } | |
| $account_roles = array_values($account->roles); | |
| return _mymodule_arrays_have_overlap($account_roles, $roles); | |
| } | |
| /** | |
| * ふたつのインデックス配列が共通の値を持つかどうかを調べる | |
| * | |
| * @param array $a1 | |
| * 重複をチェックしたい配列 1 。インデックス配列。 | |
| * @param array $a2 | |
| * 重複をチェックしたい配列 2 。インデックス配列。 | |
| * | |
| * @return bool | |
| * ふたつのインデックス配列が共通の値を持っていれば TRUE その他は FALSE | |
| */ | |
| function _mymodule_arrays_have_overlap($a1, $a2) { | |
| $overlapped = array_intersect($a1, $a2); | |
| return count($overlapped) > 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment