-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement group backend #1
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,3 +44,6 @@ nbproject | |
# Mac OS | ||
.DS_Store | ||
|
||
# test files | ||
clover.xml | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
language: php | ||
php: | ||
- 5.6 | ||
- 7.0 | ||
- 7.1 | ||
|
||
env: | ||
global: | ||
- CORE_BRANCH=master | ||
- APP_NAME=customgroups | ||
matrix: | ||
- DB=sqlite | ||
|
||
branches: | ||
only: | ||
- master | ||
|
||
before_install: | ||
- wget https://github.com/raw/owncloud/administration/master/travis-ci/before_install.sh | ||
- bash ./before_install.sh $APP_NAME $CORE_BRANCH $DB | ||
|
||
# Add some output debugging information | ||
- cd ../core | ||
- ./occ check | ||
- ./occ status | ||
- ./occ app:list | ||
|
||
script: | ||
- cd apps/$APP_NAME/ | ||
|
||
# Test the app | ||
- sh -c "if [ '$JSTESTS' != '1' -a '$CODECHECK' = '1' ]; then make test-codecheck; fi" | ||
- sh -c "if [ '$JSTESTS' != '1' -a '$CODECHECK' = '1' ]; then make test-codecheck; fi" | ||
- sh -c "if [ '$JSTESTS' != '1' -a '$CODECHECK' = '2' ]; then make test-codecheck-deprecations; fi" | ||
|
||
# Run phpunit tests | ||
- sh -c "if [ '$JSTESTS' != '1' -a '$CODECHECK' != '1' -a '$CODECHECK' != '2' ]; then make test-php; fi" | ||
|
||
# Create and upload coverage report | ||
- sh -c "if [ '$JSTESTS' != '1' -a '$CODECHECK' != '1' -a '$CODECHECK' != '2' ]; then make test-upload-coverage; fi" | ||
|
||
# Run Javascript unit tests | ||
- make test-js | ||
|
||
matrix: | ||
include: | ||
- php: 5.6 | ||
env: DB=mysql | ||
- php: 5.6 | ||
env: DB=pgsql | ||
- php: 5.6 | ||
env: DB=mysql;CODECHECK=1 | ||
- php: 5.6 | ||
env: DB=mysql;CODECHECK=2 | ||
- php: 5.6 | ||
env: DB=mysql;JSTESTS=1 | ||
allow_failures: | ||
- env: DB=mysql;CODECHECK=2 | ||
fast_finish: true |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,5 @@ | |
* | ||
*/ | ||
|
||
$app = new \OCA\CustomGroups\Application(); | ||
$app->registerGroupBackend(); |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
/** | ||
* @author Vincent Petry <pvince81@owncloud.com> | ||
* | ||
* @copyright Copyright (c) 2016, ownCloud, Inc. | ||
* @license AGPL-3.0 | ||
* | ||
* This code is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License, version 3, | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
* | ||
*/ | ||
|
||
namespace OCA\CustomGroups; | ||
|
||
use OCP\AppFramework\App; | ||
|
||
class Application extends App { | ||
public function __construct (array $urlParams = array()) { | ||
parent::__construct('customgroups', $urlParams); | ||
} | ||
|
||
/** | ||
* Register the group manager | ||
*/ | ||
public function registerGroupBackend() { | ||
$backend = $this->getContainer()->query('\OCA\CustomGroups\CustomGroupsBackend'); | ||
$this->getContainer()->getServer()->getGroupManager()->addBackend($backend); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
<?php | ||
/** | ||
* @author Vincent Petry <pvince81@owncloud.com> | ||
* | ||
* @copyright Copyright (c) 2016, ownCloud GmbH. | ||
* @license AGPL-3.0 | ||
* | ||
* This code is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License, version 3, | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
* | ||
*/ | ||
|
||
namespace OCA\CustomGroups; | ||
|
||
/** | ||
* Group backend for custom groups for integration with core | ||
*/ | ||
class CustomGroupsBackend implements \OCP\GroupInterface { | ||
|
||
const GROUP_ID_PREFIX = 'customgroup_'; | ||
|
||
/** | ||
* @var CustomGroupsDatabaseHandler | ||
*/ | ||
private $handler; | ||
|
||
public function __construct( | ||
CustomGroupsDatabaseHandler $handler | ||
) { | ||
$this->handler = $handler; | ||
} | ||
|
||
/** | ||
* Checks if backend implements actions. | ||
* | ||
* @param int $actions bitwise-or'ed actions | ||
* @return boolean | ||
*/ | ||
public function implementsActions($actions) { | ||
return ($actions & self::GROUP_DETAILS) !== 0; | ||
} | ||
|
||
/** | ||
* Checks whether the user is member of a group or not. | ||
* | ||
* @param string $uid uid of the user | ||
* @param string $gid gid of the group | ||
* @return bool | ||
*/ | ||
public function inGroup($uid, $gid) { | ||
$numericGroupId = $this->extractNumericGroupId($gid); | ||
if (is_null($numericGroupId)) { | ||
return false; | ||
} | ||
|
||
return $this->handler->inGroup($uid, $numericGroupId); | ||
} | ||
|
||
/** | ||
* Get all groups a user belongs to | ||
* | ||
* @param string $uid Name of the user | ||
* @return array an array of group names | ||
*/ | ||
public function getUserGroups($uid) { | ||
$groups = $this->handler->getUserGroups($uid); | ||
return array_map(function($numericGroupId) { | ||
return $this->formatGroupId($numericGroupId); | ||
}, $groups); | ||
} | ||
|
||
/** | ||
* Returns a list with all groups. | ||
* The search string will match any part of the display name | ||
* field. | ||
* | ||
* @param string $search search string | ||
* @param int $limit limit or -1 to disable | ||
* @param int $offset offset | ||
* @return array an array of group names | ||
* | ||
*/ | ||
public function getGroups($search = '', $limit = -1, $offset = 0) { | ||
$groups = $this->handler->searchGroups($search, $limit, $offset); | ||
return array_map(function($groupInfo) { | ||
return $this->formatGroupId($groupInfo['group_id']); | ||
}, $groups); | ||
} | ||
|
||
/** | ||
* Checks if a group exists | ||
* | ||
* @param string $gid group id | ||
* @return bool true if the group exists, false otherwise | ||
*/ | ||
public function groupExists($gid) { | ||
return !is_null($this->getGroupDetails($gid)); | ||
} | ||
|
||
/** | ||
* Returns the info for a given group. | ||
* | ||
* @param string $gid group id | ||
* @return array|null group info or null if not found | ||
*/ | ||
public function getGroupDetails($gid) { | ||
$numericGroupId = $this->extractNumericGroupId($gid); | ||
if (is_null($numericGroupId)) { | ||
return null; | ||
} | ||
|
||
$group = $this->handler->getGroup($numericGroupId); | ||
if (is_null($group)) { | ||
return null; | ||
} | ||
return [ | ||
'gid' => $this->formatGroupId($group['group_id']), | ||
'displayName' => $group['display_name'], | ||
]; | ||
} | ||
|
||
/** | ||
* Returns a list of all users in a group | ||
* | ||
* @param string $gid | ||
* @param string $search | ||
* @param int $limit | ||
* @param int $offset | ||
* @return array an array of user ids | ||
*/ | ||
public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { | ||
// not exposed to regular user management | ||
return []; | ||
} | ||
|
||
/** | ||
* Extracts the numeric id from the group id string with | ||
* the format "customgroup_$id" | ||
* | ||
* @param string $gid group id in format "customgroup_$id" | ||
* @return extracted numeric id or null if the format did not match | ||
*/ | ||
private function extractNumericGroupId($gid) { | ||
$len = strlen(self::GROUP_ID_PREFIX); | ||
$prefixPart = substr($gid, 0, $len); | ||
if ($prefixPart !== self::GROUP_ID_PREFIX) { | ||
return null; | ||
} | ||
|
||
$numericPart = substr($gid, $len); | ||
|
||
if (!is_numeric($numericPart)) { | ||
return null; | ||
} | ||
return (int)$numericPart; | ||
} | ||
|
||
/** | ||
* Formats the given numeric group id to a string | ||
* | ||
* @param int $numericId numeric group id | ||
* @return formatted id in format "customgroup_$id" | ||
*/ | ||
private function formatGroupId($numericId) { | ||
return self::GROUP_ID_PREFIX . $numericId; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know where this come from.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean ? This method is called in appinfo/app.php
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
\OCA\CustomGroups\CustomGroupsBackend
needs to be register in the container, but I don't know where this registration is made.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it doesn't. DI container power !
It works because the class name matches exactly so it's able to resolve it.
You only need explicit registration when the names different or when your constructor requires parameters that cannot be injected (ex: strings)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😮 👏