-
Notifications
You must be signed in to change notification settings - Fork 10k
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
Chore: create setAvatarFromService endpoint #26402
base: develop
Are you sure you want to change the base?
Changes from 21 commits
cbb99a9
d5b6daf
a93420b
5c896aa
8718a51
5808244
8d656d7
f3244b3
6fcfa0a
846d0ed
b25a654
b7b5dbf
fd12b3f
2340590
f8ced02
3498f94
0bcd9e0
c9d9b24
61e4faa
770c368
39e7142
ce2e59f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,14 @@ import { Match, check } from 'meteor/check'; | |
import { DDPRateLimiter } from 'meteor/ddp-rate-limiter'; | ||
|
||
import { settings } from '../../app/settings/server'; | ||
import { setUserAvatar } from '../../app/lib'; | ||
import { setUserAvatar } from '../../app/lib/server'; | ||
import { Users } from '../../app/models/server'; | ||
import { hasPermission } from '../../app/authorization/server'; | ||
import { methodDeprecationLogger } from '../../app/lib/server/lib/deprecationWarningLogger'; | ||
|
||
Meteor.methods({ | ||
setAvatarFromService(dataURI, contentType, service, userId) { | ||
setAvatarFromService(dataURI: string, contentType: string, service: string, userId: string) { | ||
methodDeprecationLogger.warn('setAvatarFromService will be deprecated in future versions of Rocket.Chat'); | ||
check(dataURI, String); | ||
check(contentType, Match.Optional(String)); | ||
check(service, Match.Optional(String)); | ||
|
@@ -29,7 +31,8 @@ Meteor.methods({ | |
let user; | ||
|
||
if (userId && userId !== Meteor.userId()) { | ||
if (!hasPermission(Meteor.userId(), 'edit-other-user-avatar')) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of having to disable this rule, you can make your code compliant. one way to achieve that is assigning const userId = Meteor.userId();
if (!userId) {
throw new Error('generic error');
}
// now you can use userId here, no need to disable a TS lint rule
if (!hasPermission(userId, 'edit-other-user-avatar')) {
// throw ...
} |
||
if (!hasPermission(Meteor.userId()!, 'edit-other-user-avatar')) { | ||
throw new Meteor.Error('error-unauthorized', 'Unauthorized', { | ||
method: 'setAvatarFromService', | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
import { BASE_URL } from '../e2e/config/constants' | ||
|
||
export const targetUser = 'rocket.cat'; | ||
felipe-rod123 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
export const imgURL = './public/images/logo/1024x1024.png'; | ||
export const avatarURL = BASE_URL + '/images/logo/1024x1024.png'; |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -751,6 +751,91 @@ describe('[Users]', function () { | |||||||||||||||||||
}); | ||||||||||||||||||||
}); | ||||||||||||||||||||
|
||||||||||||||||||||
describe('[/users.setAvatarFromService]', () => { | ||||||||||||||||||||
let user; | ||||||||||||||||||||
let userCredentials; | ||||||||||||||||||||
before(async () => { | ||||||||||||||||||||
user = await createUser(); | ||||||||||||||||||||
userCredentials = await login(user.username, password); | ||||||||||||||||||||
}); | ||||||||||||||||||||
before((done) => { | ||||||||||||||||||||
updateSetting('Accounts_AllowUserAvatarChange', true).then(() => { | ||||||||||||||||||||
updatePermission('edit-other-user-avatar', ['admin', 'user']).then(done); | ||||||||||||||||||||
}); | ||||||||||||||||||||
}); | ||||||||||||||||||||
Comment on lines
+761
to
+765
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use
Suggested change
|
||||||||||||||||||||
after(async () => { | ||||||||||||||||||||
await updateSetting('Accounts_AllowUserAvatarChange', true); | ||||||||||||||||||||
await deleteUser(user); | ||||||||||||||||||||
user = undefined; | ||||||||||||||||||||
await updatePermission('edit-other-user-avatar', ['admin']); | ||||||||||||||||||||
}); | ||||||||||||||||||||
it('should set the avatar of the logged user by a local image', (done) => { | ||||||||||||||||||||
request | ||||||||||||||||||||
.post(api('users.setAvatarFromService')) | ||||||||||||||||||||
.set(userCredentials) | ||||||||||||||||||||
.send({ | ||||||||||||||||||||
username: adminUsername, | ||||||||||||||||||||
blob: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==', | ||||||||||||||||||||
contentType: 'image/png', | ||||||||||||||||||||
service: 'gravatar', | ||||||||||||||||||||
}) | ||||||||||||||||||||
.expect('Content-Type', 'application/json') | ||||||||||||||||||||
.expect(200) | ||||||||||||||||||||
.expect((res) => { | ||||||||||||||||||||
expect(res.body).to.have.property('success', true); | ||||||||||||||||||||
}) | ||||||||||||||||||||
.end(done); | ||||||||||||||||||||
}); | ||||||||||||||||||||
it('should update the avatar of another user by userId when the logged user has the necessary permission (edit-other-user-avatar)', (done) => { | ||||||||||||||||||||
request | ||||||||||||||||||||
.post(api('users.setAvatarFromService')) | ||||||||||||||||||||
.set(userCredentials) | ||||||||||||||||||||
.send({ | ||||||||||||||||||||
username: adminUsername, | ||||||||||||||||||||
blob: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==', | ||||||||||||||||||||
contentType: 'image/png', | ||||||||||||||||||||
service: 'gravatar', | ||||||||||||||||||||
}) | ||||||||||||||||||||
.expect('Content-Type', 'application/json') | ||||||||||||||||||||
.expect(200) | ||||||||||||||||||||
.expect((res) => { | ||||||||||||||||||||
expect(res.body).to.have.property('success', true); | ||||||||||||||||||||
}) | ||||||||||||||||||||
.end(done); | ||||||||||||||||||||
}); | ||||||||||||||||||||
it('should set the avatar of another user by username and local image when the logged user has the necessary permission (edit-other-user-avatar)', (done) => { | ||||||||||||||||||||
request | ||||||||||||||||||||
.post(api('users.setAvatarFromService')) | ||||||||||||||||||||
.set(userCredentials) | ||||||||||||||||||||
.send({ | ||||||||||||||||||||
username: adminUsername, | ||||||||||||||||||||
blob: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==', | ||||||||||||||||||||
contentType: 'image/png', | ||||||||||||||||||||
service: 'gravatar', | ||||||||||||||||||||
}) | ||||||||||||||||||||
.expect('Content-Type', 'application/json') | ||||||||||||||||||||
.expect(200) | ||||||||||||||||||||
.expect((res) => { | ||||||||||||||||||||
expect(res.body).to.have.property('success', true); | ||||||||||||||||||||
}) | ||||||||||||||||||||
.end(done); | ||||||||||||||||||||
}); | ||||||||||||||||||||
it('should return an error if the correct params are not provided', (done) => { | ||||||||||||||||||||
updatePermission('edit-other-user-avatar', []).then(() => { | ||||||||||||||||||||
request | ||||||||||||||||||||
.post(api('users.setAvatarFromService')) | ||||||||||||||||||||
.set(userCredentials) | ||||||||||||||||||||
.send({}) | ||||||||||||||||||||
.expect('Content-Type', 'application/json') | ||||||||||||||||||||
.expect(400) | ||||||||||||||||||||
.expect((res) => { | ||||||||||||||||||||
expect(res.body).to.have.property('success', false); | ||||||||||||||||||||
}) | ||||||||||||||||||||
.end(done); | ||||||||||||||||||||
}); | ||||||||||||||||||||
}); | ||||||||||||||||||||
}); | ||||||||||||||||||||
|
||||||||||||||||||||
describe('[/users.resetAvatar]', () => { | ||||||||||||||||||||
let user; | ||||||||||||||||||||
before(async () => { | ||||||||||||||||||||
|
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.
there is no need to create a new endpoint.. we already have the endpoint
users.setAvatar
, if it is missing this functionality we should just improve it.