]> source.dussan.org Git - nextcloud-server.git/commitdiff
Check if user is admin - bool
authorLukas Reschke <lukas@statuscode.ch>
Mon, 14 Jan 2013 18:45:17 +0000 (19:45 +0100)
committerLukas Reschke <lukas@statuscode.ch>
Mon, 14 Jan 2013 18:45:17 +0000 (19:45 +0100)
There was no "isAdminUser()" function which returned bool. This is
irritiating as there were a loooooooot of places in the code which
checked this itself with `OC_Group::inGroup($uid, 'admin)` - why not
use a function for this?
(Especially if you consider that we might change the group name in the
future, which would lead to problems then)

Additionally, @Raydiation needed such a method for his AppFramework :)

16 files changed:
lib/api.php
lib/app.php
lib/json.php
lib/migrate.php
lib/ocs/cloud.php
lib/subadmin.php
lib/user.php
lib/util.php
settings/ajax/changepassword.php
settings/ajax/createuser.php
settings/ajax/removeuser.php
settings/ajax/setquota.php
settings/ajax/togglegroups.php
settings/ajax/userlist.php
settings/help.php
settings/users.php

index cb67e0c2a892f33305e7e74b1146dc4dd50b1f8d..c464cd049133b64047cba7570112148abec4fe62 100644 (file)
@@ -127,8 +127,7 @@ class OC_API {
                                        return false;
                                } else {
                                        $subAdmin = OC_SubAdmin::isSubAdmin($user);
-                                       $admin = OC_Group::inGroup($user, 'admin');
-                                       if($subAdmin || $admin) {
+                                       if($subAdmin) {
                                                return true;
                                        } else {
                                                return false;
@@ -141,7 +140,7 @@ class OC_API {
                                if(!$user) {
                                        return false;
                                } else {
-                                       return OC_Group::inGroup($user, 'admin');
+                                       return OC_User::isAdminUser($user);
                                }
                                break;
                        default:
index e60bce2a201e9c775f103daf96b7e455c425c874..3b039733fe5345bd101d4d83844b00bd81c95a25 100644 (file)
@@ -313,14 +313,14 @@ class OC_App{
                                $settings[]=array( "id" => "settings", "order" => 1000, "href" => OC_Helper::linkToRoute( "settings_settings" ), "name" => $l->t("Settings"), "icon" => OC_Helper::imagePath( "settings", "settings.svg" ));
 
                        //SubAdmins are also allowed to access user management
-                       if(OC_SubAdmin::isSubAdmin($_SESSION["user_id"]) || OC_Group::inGroup( $_SESSION["user_id"], "admin" )) {
+                       if(OC_SubAdmin::isSubAdmin(OC_User::getUser())) {
                                // admin users menu
                                $settings[] = array( "id" => "core_users", "order" => 2, "href" => OC_Helper::linkToRoute( "settings_users" ), "name" => $l->t("Users"), "icon" => OC_Helper::imagePath( "settings", "users.svg" ));
                        }
 
 
                        // if the user is an admin
-                       if(OC_Group::inGroup( $_SESSION["user_id"], "admin" )) {
+                       if(OC_User::isAdminUser(OC_User::getUser())) {
                                // admin apps menu
                                $settings[] = array( "id" => "core_apps", "order" => 3, "href" => OC_Helper::linkToRoute( "settings_apps" ).'?installed', "name" => $l->t("Apps"), "icon" => OC_Helper::imagePath( "settings", "apps.svg" ));
 
index 204430411c09b28a7925dafc209529cfcebe3eb1..5ab877540d4342c246510d80fdaa6cb89d023d29 100644 (file)
@@ -57,9 +57,7 @@ class OC_JSON{
        * Check if the user is a admin, send json error msg if not
        */
        public static function checkAdminUser() {
-               self::checkLoggedIn();
-               self::verifyUser();
-               if( !OC_Group::inGroup( OC_User::getUser(), 'admin' )) {
+               if( !OC_User::isAdminUser(OC_User::getUser())) {
                        $l = OC_L10N::get('lib');
                        self::error(array( 'data' => array( 'message' => $l->t('Authentication error') )));
                        exit();
@@ -70,9 +68,7 @@ class OC_JSON{
        * Check if the user is a subadmin, send json error msg if not
        */
        public static function checkSubAdminUser() {
-               self::checkLoggedIn();
-               self::verifyUser();
-               if(!OC_Group::inGroup(OC_User::getUser(), 'admin') && !OC_SubAdmin::isSubAdmin(OC_User::getUser())) {
+               if(!OC_SubAdmin::isSubAdmin(OC_User::getUser())) {
                        $l = OC_L10N::get('lib');
                        self::error(array( 'data' => array( 'message' => $l->t('Authentication error') )));
                        exit();
index 5ff8e338a442b887517858f709ca77a402ccabf9..62ab00bc332d776bf7bf1f234ff95cd54224360a 100644 (file)
@@ -219,7 +219,7 @@ class OC_Migrate{
 
                // We need to be an admin if we are not importing our own data
                if(($type == 'user' && self::$uid != $currentuser) || $type != 'user' ) {
-                       if( !OC_Group::inGroup( OC_User::getUser(), 'admin' )) {
+                       if( !OC_User::isAdminUser($currentuser)) {
                                // Naughty.
                                OC_Log::write( 'migration', 'Import not permitted.', OC_Log::ERROR );
                                return json_encode( array( 'success' => false ) );
index 21095ec91e9991a3a647ed79c6279001eaf96add..4564a1e9f1f8646010e0bffb11265babfc9399ae 100644 (file)
@@ -40,7 +40,7 @@ class OC_OCS_Cloud {
        
        public static function getUserQuota($parameters) {
                $user = OC_User::getUser();
-               if(OC_Group::inGroup($user, 'admin') or ($user==$parameters['user'])) {
+               if(OC_User::isAdminUser($user) or ($user==$parameters['user'])) {
 
                        if(OC_User::userExists($parameters['user'])) {
                                // calculate the disc space
@@ -82,7 +82,7 @@ class OC_OCS_Cloud {
        
        public static function getUserPrivatekey($parameters) {
                $user = OC_User::getUser();
-               if(OC_Group::inGroup($user, 'admin') or ($user==$parameters['user'])) {
+               if(OC_User::isAdminUser($user) or ($user==$parameters['user'])) {
 
                        if(OC_User::userExists($user)) {
                                // calculate the disc space
index 9e83e6da430f166e3d088778c8c129e56b15687e..8cda7240ac92f8865ae61f3d6102129ec4aba5f1 100644 (file)
@@ -122,6 +122,11 @@ class OC_SubAdmin{
         * @return bool
         */
        public static function isSubAdmin($uid) {
+               // Check if the user is already an admin
+               if(OC_Group::inGroup($uid, 'admin' )) {
+                       return true;
+               }
+
                $stmt = OC_DB::prepare('SELECT COUNT(*) AS `count` FROM `*PREFIX*group_admin` WHERE `uid` = ?');
                $result = $stmt->execute(array($uid));
                $result = $result->fetchRow();
@@ -141,7 +146,7 @@ class OC_SubAdmin{
                if(!self::isSubAdmin($subadmin)) {
                        return false;
                }
-               if(OC_Group::inGroup($user, 'admin')) {
+               if(OC_User::isAdminUser($user)) {
                        return false;
                }
                $accessiblegroups = self::getSubAdminsGroups($subadmin);
index 80f88ca7052da3e46dc98ac60da179a7680399ac..8d4eb7aec4d29b82cfad8788f0fa517a175d188c 100644 (file)
@@ -299,6 +299,19 @@ class OC_User {
                return false;
        }
 
+       /**
+        * @brief Check if the user is an admin user
+        * @param $uid uid of the admin
+        * @returns bool
+        */
+       public static function isAdminUser($uid) {
+               if(OC_Group::inGroup($uid, 'admin' )) {
+                       return true;
+               }
+               return false;
+       }
+
+
        /**
         * @brief get the user id of the user currently logged in.
         * @return string uid or false
index 7b1de094eade60887a44d44438c2a4a7dd2cc70b..06ae37176c10a53d877edaa9ec75372237581ab9 100755 (executable)
@@ -342,10 +342,7 @@ class OC_Util {
         * Check if the user is a admin, redirects to home if not
         */
        public static function checkAdminUser() {
-               // Check if we are a user
-               self::checkLoggedIn();
-               self::verifyUser();
-               if( !OC_Group::inGroup( OC_User::getUser(), 'admin' )) {
+               if( !OC_User::isAdminUser(OC_User::getUser())) {
                        header( 'Location: '.OC_Helper::linkToAbsolute( '', 'index.php' ));
                        exit();
                }
@@ -356,12 +353,6 @@ class OC_Util {
         * @return array $groups where the current user is subadmin
         */
        public static function checkSubAdminUser() {
-               // Check if we are a user
-               self::checkLoggedIn();
-               self::verifyUser();
-               if(OC_Group::inGroup(OC_User::getUser(), 'admin')) {
-                       return true;
-               }
                if(!OC_SubAdmin::isSubAdmin(OC_User::getUser())) {
                        header( 'Location: '.OC_Helper::linkToAbsolute( '', 'index.php' ));
                        exit();
index b2db2611518182df78ef760b1350561ff5e9530b..cf440c577da53e344242c017fa4564db6484c31b 100644 (file)
@@ -9,7 +9,7 @@ $password = $_POST["password"];
 $oldPassword=isset($_POST["oldpassword"])?$_POST["oldpassword"]:'';
 
 $userstatus = null;
-if(OC_Group::inGroup(OC_User::getUser(), 'admin')) {
+if(OC_User::isAdminUser(OC_User::getUser()) {
        $userstatus = 'admin';
 }
 if(OC_SubAdmin::isUserAccessible(OC_User::getUser(), $username)) {
index addae78517a45c51de930517355eb8a41ba42d23..09ef25d92fa1f6835e5dc0617daca43bf5bf06c4 100644 (file)
@@ -3,9 +3,7 @@
 OCP\JSON::callCheck();
 OC_JSON::checkSubAdminUser();
 
-$isadmin = OC_Group::inGroup(OC_User::getUser(), 'admin')?true:false;
-
-if($isadmin) {
+if(OC_User::isAdminUser(OC_User::getUser())) {
        $groups = array();
        if( isset( $_POST["groups"] )) {
                $groups = $_POST["groups"];
index 9ffb32a0b23fa5704cf69dba540c134e93f65960..bf3a34f1472ffae747cc165f24af756af3a0a2e9 100644 (file)
@@ -10,7 +10,7 @@ if(OC_User::getUser() === $username) {
        exit;
 }
 
-if(!OC_Group::inGroup(OC_User::getUser(), 'admin') && !OC_SubAdmin::isUserAccessible(OC_User::getUser(), $username)) {
+if(!OC_User::isAdminUser(OC_User::getUser()) && !OC_SubAdmin::isUserAccessible(OC_User::getUser(), $username)) {
        $l = OC_L10N::get('core');
        OC_JSON::error(array( 'data' => array( 'message' => $l->t('Authentication error') )));
        exit();
index 845f8ea408c82da7d328cfcee6b1e2fdcae9096b..89c46294d087c7fccf5bc05589ade728bf9bbd35 100644 (file)
@@ -10,7 +10,7 @@ OCP\JSON::callCheck();
 
 $username = isset($_POST["username"])?$_POST["username"]:'';
 
-if(($username == '' && !OC_Group::inGroup(OC_User::getUser(), 'admin')) || (!OC_Group::inGroup(OC_User::getUser(), 'admin') && !OC_SubAdmin::isUserAccessible(OC_User::getUser(), $username))) {
+if(($username == '' && !OC_User::isAdminUser($user)) || (!OC_User::isAdminUser(OC_User::getUser()) && !OC_SubAdmin::isUserAccessible(OC_User::getUser(), $username))) {
        $l = OC_L10N::get('core');
        OC_JSON::error(array( 'data' => array( 'message' => $l->t('Authentication error') )));
        exit();
index 83d455550aee38c23e419ca7feec907e0aa35acb..548dc2e209bea273bf805b19dfdbc1a7d093454f 100644 (file)
@@ -7,13 +7,13 @@ $success = true;
 $username = $_POST["username"];
 $group = $_POST["group"];
 
-if($username == OC_User::getUser() && $group == "admin" &&  OC_Group::inGroup($username, 'admin')) {
+if($username == OC_User::getUser() && $group == "admin" &&  OC_User::isAdminUser($username)) {
        $l = OC_L10N::get('core');
        OC_JSON::error(array( 'data' => array( 'message' => $l->t('Admins can\'t remove themself from the admin group'))));
        exit();
 }
 
-if(!OC_Group::inGroup(OC_User::getUser(), 'admin') && (!OC_SubAdmin::isUserAccessible(OC_User::getUser(), $username) || !OC_SubAdmin::isGroupAccessible(OC_User::getUser(), $group))) {
+if(!OC_User::isAdminUser(OC_User::getUser()) && (!OC_SubAdmin::isUserAccessible(OC_User::getUser(), $username) || !OC_SubAdmin::isGroupAccessible(OC_User::getUser(), $group))) {
        $l = OC_L10N::get('core');
        OC_JSON::error(array( 'data' => array( 'message' => $l->t('Authentication error') )));
        exit();
index eaeade60a39014ef423362e117e496942ba7de91..9bbff80ea0cc17eef7bccd0ae2e13f699b093fea 100644 (file)
@@ -28,7 +28,7 @@ if (isset($_GET['offset'])) {
        $offset = 0;
 }
 $users = array();
-if (OC_Group::inGroup(OC_User::getUser(), 'admin')) {
+if (OC_User::isAdminUser(OC_User::getUser())) {
        $batch = OC_User::getUsers('', 10, $offset);
        foreach ($batch as $user) {
                $users[] = array(
index cd3d615425ca1b651a153c207f1936b2cf603d08..a5ac11ec9a3e0e41d5662a90e4a0b32a5ff27ea4 100644 (file)
@@ -27,7 +27,7 @@ $url1=OC_Helper::linkToRoute( "settings_help" ).'?mode=user';
 $url2=OC_Helper::linkToRoute( "settings_help" ).'?mode=admin';
 
 $tmpl = new OC_Template( "settings", "help", "user" );
-$tmpl->assign( "admin", OC_Group::inGroup(OC_User::getUser(), 'admin') );
+$tmpl->assign( "admin", OC_User::isAdminUser(OC_User::getUser()));
 $tmpl->assign( "url", $url );
 $tmpl->assign( "url1", $url1 );
 $tmpl->assign( "url2", $url2 );
index 07a7620d3c04b7d37b2010492037c9b3ed232c1f..1a32a7ecb5b6f7ed9d13197db1d73f2fc05376bd 100644 (file)
@@ -18,8 +18,7 @@ OC_App::setActiveNavigationEntry( 'core_users' );
 $users = array();
 $groups = array();
 
-$isadmin = OC_Group::inGroup(OC_User::getUser(), 'admin')?true:false;
-if($isadmin) {
+if(OC_User::isAdminUser(OC_User::getUser())) {
        $accessiblegroups = OC_Group::getGroups();
        $accessibleusers = OC_User::getUsers('', 30);
        $subadmins = OC_SubAdmin::getAllSubAdmins();