]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Profile.php
Merge branch 'admin-sections/4' into 0.9.x
[quix0rs-gnu-social.git] / classes / Profile.php
index 2668efcc70cd4bd3f524eeb7048f018815c0a37d..4b2e0900647b29a20204ed63345a6665e2df69c5 100644 (file)
@@ -310,10 +310,12 @@ class Profile extends Memcached_DataObject
           'AND subscription.subscribed != subscription.subscriber ' .
           'ORDER BY subscription.created DESC ';
 
-        if (common_config('db','type') == 'pgsql') {
-            $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
-        } else {
-            $qry .= ' LIMIT ' . $offset . ', ' . $limit;
+        if ($offset>0 && !is_null($limit)){
+            if (common_config('db','type') == 'pgsql') {
+                $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
+            } else {
+                $qry .= ' LIMIT ' . $offset . ', ' . $limit;
+            }
         }
 
         $profile = new Profile();
@@ -333,11 +335,13 @@ class Profile extends Memcached_DataObject
           'AND subscription.subscribed != subscription.subscriber ' .
           'ORDER BY subscription.created DESC ';
 
-        if ($offset) {
-            if (common_config('db','type') == 'pgsql') {
-                $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
-            } else {
-                $qry .= ' LIMIT ' . $offset . ', ' . $limit;
+        if ($offset>0 && !is_null($limit)){
+            if ($offset) {
+                if (common_config('db','type') == 'pgsql') {
+                    $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
+                } else {
+                    $qry .= ' LIMIT ' . $offset . ', ' . $limit;
+                }
             }
         }
 
@@ -590,18 +594,23 @@ class Profile extends Memcached_DataObject
 
     function hasRole($name)
     {
-        $role = User_role::pkeyGet(array('user_id' => $this->id,
-                                         'role' => $name));
-        return (!empty($role));
+        $has_role = false;
+        if (Event::handle('StartHasRole', array($this, $name, &$has_role))) {
+            $role = Profile_role::pkeyGet(array('profile_id' => $this->id,
+                                                'role' => $name));
+            $has_role = !empty($role);
+            Event::handle('EndHasRole', array($this, $name, $has_role));
+        }
+        return $has_role;
     }
 
     function grantRole($name)
     {
-        $role = new User_role();
+        $role = new Profile_role();
 
-        $role->user_id = $this->id;
-        $role->role    = $name;
-        $role->created = common_sql_now();
+        $role->profile_id = $this->id;
+        $role->role       = $name;
+        $role->created    = common_sql_now();
 
         $result = $role->insert();
 
@@ -615,8 +624,8 @@ class Profile extends Memcached_DataObject
 
     function revokeRole($name)
     {
-        $role = User_role::pkeyGet(array('user_id' => $this->id,
-                                         'role' => $name));
+        $role = Profile_role::pkeyGet(array('profile_id' => $this->id,
+                                            'role' => $name));
 
         if (empty($role)) {
             throw new Exception('Cannot revoke role "'.$name.'" for user #'.$this->id.'; does not exist.');
@@ -634,31 +643,77 @@ class Profile extends Memcached_DataObject
 
     function isSandboxed()
     {
-        return $this->hasRole(User_role::SANDBOXED);
+        return $this->hasRole(Profile_role::SANDBOXED);
     }
 
     function isSilenced()
     {
-        return $this->hasRole(User_role::SILENCED);
+        return $this->hasRole(Profile_role::SILENCED);
     }
 
     function sandbox()
     {
-        $this->grantRole(User_role::SANDBOXED);
+        $this->grantRole(Profile_role::SANDBOXED);
     }
 
     function unsandbox()
     {
-        $this->revokeRole(User_role::SANDBOXED);
+        $this->revokeRole(Profile_role::SANDBOXED);
     }
 
     function silence()
     {
-        $this->grantRole(User_role::SILENCED);
+        $this->grantRole(Profile_role::SILENCED);
     }
 
     function unsilence()
     {
-        $this->revokeRole(User_role::SILENCED);
+        $this->revokeRole(Profile_role::SILENCED);
+    }
+
+    /**
+     * Does this user have the right to do X?
+     *
+     * With our role-based authorization, this is merely a lookup for whether the user
+     * has a particular role. The implementation currently uses a switch statement
+     * to determine if the user has the pre-defined role to exercise the right. Future
+     * implementations may allow per-site roles, and different mappings of roles to rights.
+     *
+     * @param $right string Name of the right, usually a constant in class Right
+     * @return boolean whether the user has the right in question
+     */
+
+    function hasRight($right)
+    {
+        $result = false;
+        if (Event::handle('UserRightsCheck', array($this, $right, &$result))) {
+            switch ($right)
+            {
+            case Right::DELETEOTHERSNOTICE:
+            case Right::SANDBOXUSER:
+            case Right::SILENCEUSER:
+            case Right::DELETEUSER:
+                $result = $this->hasRole(Profile_role::MODERATOR);
+                break;
+            case Right::CONFIGURESITE:
+                $result = $this->hasRole(Profile_role::ADMINISTRATOR);
+                break;
+            case Right::NEWNOTICE:
+            case Right::NEWMESSAGE:
+            case Right::SUBSCRIBE:
+                $result = !$this->isSilenced();
+                break;
+            case Right::PUBLICNOTICE:
+            case Right::EMAILONREPLY:
+            case Right::EMAILONSUBSCRIBE:
+            case Right::EMAILONFAVE:
+                $result = !$this->isSandboxed();
+                break;
+            default:
+                $result = false;
+                break;
+            }
+        }
+        return $result;
     }
 }