Skip to content
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

Detect existing email/user on frontend and backend #168

Merged
merged 2 commits into from
Oct 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions controller/userscontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ public function create($email, $displayName) {
);
}

$users = $this->userManager->getByEmail($email);
if (!empty($users)) {
$errorMessages['email'] = (string)$this->l10n->t(
'A username with that email already exists.'
);
}

if (!empty($errorMessages)) {
return new DataResponse(
[
Expand Down
43 changes: 31 additions & 12 deletions js/guestshare.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ OC.Plugins.register('OC.Share.ShareDialogView', {
var oldHandler = obj.autocompleteHandler;
obj.autocompleteHandler = function(search, response) {

return oldHandler.call(obj, search, function(result) {
return oldHandler.call(obj, search, function(result, xhrResult) {
var searchTerm = search.term.trim();

// Add potential guests to the suggestions
Expand All @@ -101,16 +101,35 @@ OC.Plugins.register('OC.Share.ShareDialogView', {
result = [];
}

// only add guest entry suggestion if there isn't another matching user share entry already
var lowerSearchTerm = searchTerm.toLowerCase();
if (!_.find(result, function(entry) {
if (entry && entry.value
&& entry.value.shareType === OC.Share.SHARE_TYPE_USER
&& entry.value.shareWith.toLowerCase() === lowerSearchTerm) {
return true;
// only allow guest creation entry if there is no exact match (by user id or email, decided by the server)
var provideGuestEntry = false;

if (xhrResult
&& xhrResult.ocs.meta.statuscode === 100
&& xhrResult.ocs.data.exact.users.length === 0
) {
provideGuestEntry = true;
}

// compatibility with OC <= 10.0.3 where xhrResult is not available
// here we always show the entry as we don't know about exact matches,
// and the backend might block the request if the guest is referring
// to an existing email address
if (!xhrResult) {
var lowerSearchTerm = searchTerm.toLowerCase();
if (!_.find(result, function(entry) {
if (entry && entry.value
&& entry.value.shareType === OC.Share.SHARE_TYPE_USER
&& entry.value.shareWith.toLowerCase() === lowerSearchTerm) {
return true;
}
return false;
})) {
provideGuestEntry = true;
}
return false;
})) {
}

if (provideGuestEntry) {
result.push({
label: t('core', 'Add {unknown} (guest)', {unknown: searchTerm}),
value: {
Expand All @@ -119,9 +138,9 @@ OC.Plugins.register('OC.Share.ShareDialogView', {
}
});
}
response(result);
response(result, xhrResult);
}
response(result);
response(result, xhrResult);
});
};

Expand Down
11 changes: 11 additions & 0 deletions tests/integration/guests_features/Guests.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ Scenario: Creating a guest user works fine
Then the HTTP status code should be "201"
And check that user "guest" is a guest

Scenario: Cannot create a guest if a user with the same email address exists
Given as an "admin"
And user "existing-user" exists
When sending "PUT" to "/cloud/users/existing-user" with
| key | email |
| value | [email protected] |
When user "admin" creates guest user "guest" with email "[email protected]"
Then the HTTP status code should be "422"
# TODO: missing appropriate step in core / Provisioning
#And check that user "guest" does not exist

Scenario: A guest user cannot upload files
Given as an "admin"
And user "admin" creates guest user "guest" with email "[email protected]"
Expand Down