Hi,
I am not that familiar with PHP and so the API documentation is hard for me to understand. Within an app I need to retrieve a list of users and groups as JSON. Generally I got this working but not by using OCP\IGroupManager because I don't have a clue how to utilize it. Maybe somebody can help me with sample code? On the other hand I'd like to have this function only available to admins but I could not find a way to read out the isAdmin value with PHP.
What I did is more or less a workaround and I would appreciate any help to get it more OC-API-ish. Below is the code of my work around based on the app development tutorial.
controller/groupusercontroller.php
<?php
namespace OCA\OwnCollab_GanttChart\Controller;
use OCP\IRequest;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Controller;
use OCA\OwnCollab_GanttChart\Service\GroupUserService;
class GroupUserController extends Controller {
private $service;
private $userId;
use Errors;
/**
* @NoAdminRequired
* @param IGroupManager $groupManager
* @param bool $isAdmin
*/
public function __construct($AppName, IRequest $request,
GroupUserService $service, $UserId){
parent::__construct($AppName, $request);
$this->service = $service;
$this->userId = $UserId;
}
/**
* @NoAdminRequired
*/
public function json() {
return new JSONResponse($this->service->findAll());
}
/**
* @NoAdminRequired
*/
public function index() {
return new DataResponse($this->service->findAll());
}
/**
* @NoAdminRequired
*
* @param int $gid
*/
public function show($gid) {
return $this->handleNotFound(function () use ($gid) {
return $this->service->find($id);
});
}
}
service/groupuserservice.php
<?php
namespace OCA\OwnCollab_GanttChart\Service;
use Exception;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\IGroupManager;
use OCA\OwnCollab_GanttChart\Db\GroupUser;
use OCA\OwnCollab_GanttChart\Db\GroupUserMapper;
class GroupUserService {
private $mapper;
public function __construct(GroupUserMapper $mapper){
$this->mapper = $mapper;
}
public function findAll() {
return $this->mapper->findAll();
}
private function handleException ($e) {
if ($e instanceof DoesNotExistException ||
$e instanceof MultipleObjectsReturnedException) {
throw new NotFoundException($e->getMessage());
} else {
throw $e;
}
}
public function find($gid) {
try {
return $this->mapper->find($gid);
// in order to be able to plug in different storage backends like files
// for instance it is a good idea to turn storage related exceptions
// into service related exceptions so controllers and service users
// have to deal with only one type of exception
} catch(Exception $e) {
$this->handleException($e);
}
}
}
db/groupuser.php
<?php
namespace OCA\OwnCollab_GanttChart\Db;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
class GroupUser extends Entity implements JsonSerializable {
protected $gid;
protected $uid;
public function jsonSerialize() {
return [
'gid' => $this->gid,
'uid' => $this->uid;
];
}
}
db/groupusermapper.php
<?php
namespace OCA\OwnCollab_GanttChart\Db;
use OCP\IDb;
use OCP\AppFramework\Db\Mapper;
class GroupUserMapper extends Mapper {
public function __construct(IDb $db) {
parent::__construct($db, 'group_user', '\OCA\OwnCollab_GanttChart\Db\GroupUser');
}
public function findAll() {
$sql = 'SELECT * FROM *PREFIX*group_user';
return $this->db->executeQuery($sql)->fetchAll();
}
public function find($gid) {
$sql = 'SELECT * FROM *PREFIX*group_user WHERE gid = ?';
return $this->executeQuery($sql, [$gid]);
}
}
JS
if (OC.isUserAdmin() === true){
OCGantt.groupusers.loadAll().done(function(){});
}
OCGantt.GroupUsers = function (baseUrl) {
this._baseUrl = baseUrl;
this._groupusers = [];
this._activeGroupUser = undefined;
};
OCGantt.GroupUsers.prototype = {
load: function (id) {
var self = this;
this._groupusers.forEach(function (groupuser) {
if (groupuser.gid === gid) {
groupuser.active = true;
self._activeGroupUser = groupuser;
} else {
groupuser.active = false;
}
});
},
getActive: function () {
return this._activeGroupUser;
},
getAll: function () {
return this._groupusers;
},
loadAll: function () {
var deferred = $.Deferred();
var self = this;
$.get(this._baseUrl).done(function (groupusers) {
self._activeGroupUser = undefined;
self._groupusers = groupusers;
deferred.resolve();
}).fail(function () {
deferred.reject();
});
return deferred.promise();
},
};