Add permission to a user

@param   string  $user
@param   string  $action
@return  void
192139
public function add_user_permission($user, $action)
	{
		$db =  \App::get('db');

		if ($user == 'anonymous')
		{
			$user = '0';
		}

		if (!is_numeric($user))
		{
			$query = "SELECT id FROM `#__users` WHERE username=" . $db->quote($user) . ";";
			$db->setQuery($query);
			$user_id = $db->loadResult();

			if ($user_id === false)
			{
				$this->_error("Unknown user $user");
				return false;
			}
		}
		else
		{
			$user_id = $user;
		}

		$quoted_project_id = $db->quote($this->id);
		$quoted_user_id = $db->quote($user_id);
		$values = '';

		foreach ((array) $action as $a)
		{
			if (!empty($values))
			{
				$values .= ',';
			}
			$values .= "($quoted_project_id,$quoted_user_id," . $db->quote($a) .")";
		}

		$query = "INSERT IGNORE INTO `#__trac_user_permission` (trac_project_id,user_id,action) VALUES " .  $values . ";";

		$db->setQuery($query);
		$db->query();
	}
953849
public function hasAccess(User $user, $controller, $action, $requestParameters) {
        $required = array();
        try {
            // $rights = ServiceManager::getServiceManager()->get(RouteAnnotationService::SERVICE_ID)->getRights($controller, $action);
            // todo use $rights when PHPDoc Annotations will be moved to the Doctrines annotations
            $requiredRights = ControllerHelper::getRequiredRights($controller, $action);
            $uris = $this->extractAndGroupUriFromParameters($requestParameters, array_keys($requiredRights));

            foreach($uris as $name => $urisValue) {
                $required[] = array_fill_keys($urisValue, $requiredRights[$name]);
            }
        } catch (ActionNotFoundException $e) {
            // action not found, no access
            return false;
        }
        
        return empty($required)
            ? true
            : $this->hasPrivileges($user, array_merge(...$required));
    }
243052
protected function allowSinglePermission($permission, $object, $user)
	{
		// check to see if the permission exists
		$permObject = $this->permission->findByPermissionAndObject($permission, $object);
		
		// if it doesn't, try to create it
		if(!$permObject)
		{
			$listOfPermissions = $object->getPermissions();
			
			// make sure the requested permission is valid on the model
			if(!isset($listOfPermissions[$permission]))
			{
				throw new \Exception("Permission ".$permission." doesn't exist on the ".get_class($object)." object.");
			}
			
			// create the permission
			$permObject = $this->permission->create(array(
				'name'			=> $listOfPermissions[$permission],
				'object_type'	=> get_class($object),
				'codename'		=> $permission,
			));
			
		}
		
		// check to see if the user already has permission on the object
		$uop = $this->userObjectPermission->findPermission($user, $object, $permObject);
		
		// if they don't, give them permission
		if(!$uop)
		{
			$uop = $this->userObjectPermission->create(array(
				'user_id'		=> $user->id,
				'object_type'	=> get_class($object),
				'object_id'		=> $object->id,
				'permission_id'	=> $permObject->id,
			));
		}
	}
379172
public static function loggedCanDo($action, AbstractUser $object=null) {
		global $USER;
		return !empty($USER) && $USER->canDo($action, $object);
	}
743451
public function apply(callable $action)
    {
        foreach ($this->each() as $k => $v) {
            call_user_func($action, $v, $k);
        }
    }
1187022
function addPermission(rbac, action, resource, cb) {
  rbac.getPermission(action, resource, (err, permission) => {
    if (err) {
      return cb(err);
    }

    if (!permission) {
      return cb(new Error('Permission not exists'));
    }

    if (indexOf(this.permissions, permission.name) !== -1) {
      return cb(new Error('Permission is already assigned'));
    }

    this.permissions.push(permission.name);
    return this.save((err2, user) => {
      if (err2) {
        return cb(err2);
      }

      if (!user) {
        return cb(new Error('User is undefined'));
      }

      return cb(null, true);
    });
  });

  return this;
}
559810
protected function authorize($user, Gate $gate, string $ability, array $args): void
    {
        $can = $gate->forUser($user)->check($ability, $args);

        if (! $can) {
            throw new AuthorizationException(
                "You are not authorized to access {$this->definitionNode->name->value}"
            );
        }
    }
558229
public static function accessPossible(User $user, $controller, $action) {
        return self::getImplementation()->accessPossible($user, $controller, $action);
    }
209202
public function authorize($action, $model = null)
    {
        // for most cases it is enough to set
        // permissions in Resource object.
        if (method_exists($this->module, 'authorize')) {
            return $this->module->authorize($action, $model);
        }

        // Ask Actions Service for action permissions.
        return $this->service->authorize($action, $model, $this->module);
    }
870263
protected function setPermissions($permissions)
    {
        foreach ($permissions as $action => $permission) {
            $this->setPermission($action, $permission);
        }
    }
841308
public function allowUser($permissions, $object, $user = null)
	{
		// if no user is specified, default to the Auth one
		if(is_null($user))
		{
			$user = app('auth')->user();
		}
		
		if(!is_array($permissions))
		{
			$permissions = array($permissions);
		}
		
		foreach($permissions as $permission)
		{
			$this->allowSinglePermission($permission, $object, $user);
		}
	}
11771
public function edit_load_user_data($user) {
        if ($this->data !== null) {
            $user->{$this->inputname} = $this->data;
        }
    }
506755
public function deletePermission(string $action): void
    {
        /** @var HCAclPermission $permission */
        $permission = $this->findOneBy(['action' => $action]);

        $permission->roles()->detach();

        $permission->forceDelete();
    }
102040
public function revokeUserAccess($resource, string $action)
    {
        $resourceRule = $this->getUserPermission($resource);

        if (!$resourceRule) {
            return;
        }

        // make sure we can work with this
        if ($resourceRule && !($resourceRule instanceof UserPermissionInterface)) {
            throw new PermissionExpectedException(UserPermissionInterface::class, \get_class($resourceRule));
        }

        if ($resourceRule) {
            if (!\in_array($action, $resourceRule->getActions(), true)) {
                return;
            }
            $resourceRule->removeAction($action);
            $this->userPermissions->update($resourceRule);
        }
    }
1358112
public function hasAccess(User $user, $controller, $action, $parameters) {
        return self::accessPossible($user, $controller, $action);
    }
1188182
public function addPermission(Request $request)
    {
        $user = \App\User::findorfail($request->user_id);
        $user->givePermissionTo($request->permission_name);

        return redirect('scaffold-users/edit/'.$request->user_id);
    }
52759
public function deletePermissionForUser($user, ...$permission)
    {
        $params = [];

        $params[] = $user;
        foreach ($permission as $perm) {
            $params[] = $perm;
        }

        return $this->removePolicy(...$params);
    }