parent
d1b62bd17b
commit
5443507df8
@ -0,0 +1,3 @@ |
||||
RocketChat.checkEmailAvailability = function(email) { |
||||
return !Meteor.users.findOne({ "emails.address": { $regex : new RegExp("^" + s.trim(s.escapeRegExp(email)) + "$", "i") } }) |
||||
} |
@ -0,0 +1,9 @@ |
||||
Meteor.methods({ |
||||
clearRequestPasswordChange: function() { |
||||
if (!Meteor.userId()) { |
||||
throw new Meteor.Error('invalid-user', "[methods] clearRequestPasswordChange -> Invalid user"); |
||||
} |
||||
|
||||
return RocketChat.models.Users.unsetRequirePasswordChange(Meteor.userId()); |
||||
} |
||||
}) |
@ -0,0 +1,53 @@ |
||||
Meteor.methods |
||||
insertOrUpdateUser: (userData) -> |
||||
if not Meteor.userId() |
||||
throw new Meteor.Error('invalid-user', "[methods] updateUser -> Invalid user") |
||||
|
||||
user = Meteor.user() |
||||
|
||||
canEditUser = RocketChat.authz.hasPermission( user._id, 'edit-other-user-info') |
||||
canAddUser = RocketChat.authz.hasPermission( user._id, 'add-user') |
||||
|
||||
if userData._id and user._id isnt userData._id and canEditUser isnt true |
||||
throw new Meteor.Error 'not-authorized', '[methods] updateUser -> Not authorized' |
||||
|
||||
if not userData._id and canAddUser isnt true |
||||
throw new Meteor.Error 'not-authorized', '[methods] updateUser -> Not authorized' |
||||
|
||||
unless userData.name |
||||
throw new Meteor.Error 'name-is-required', 'Name field is required' |
||||
|
||||
unless userData.username |
||||
throw new Meteor.Error 'user-name-is-required', 'Username field is required' |
||||
|
||||
if not userData._id and not userData.password |
||||
throw new Meteor.Error 'password-is-required', 'Password is required when adding a user' |
||||
|
||||
if not userData._id |
||||
if not RocketChat.checkUsernameAvailability userData.username |
||||
throw new Meteor.Error 'username-unavailable', "#{username} is already in use :(" |
||||
|
||||
if userData.email and not RocketChat.checkEmailAvailability userData.email |
||||
throw new Meteor.Error 'username-unavailable', "#{username} is already in use :(" |
||||
|
||||
# insert user |
||||
createUser = { username: userData.username, password: userData.password } |
||||
if userData.email |
||||
createUser.email = userData.email |
||||
|
||||
_id = Accounts.createUser(createUser) |
||||
if userData.requirePasswordChange |
||||
Meteor.users.update { _id: _id }, { $set: { name: userData.name, requirePasswordChange: userData.requirePasswordChange } } |
||||
|
||||
else |
||||
#update user |
||||
Meteor.users.update { _id: userData._id }, { $set: { name: userData.name, requirePasswordChange: userData.requirePasswordChange } } |
||||
|
||||
Meteor.runAsUser userData._id, -> |
||||
Meteor.call 'setUsername', userData.username |
||||
|
||||
canEditUserPassword = RocketChat.authz.hasPermission( user._id, 'edit-other-user-password') |
||||
if canEditUserPassword and userData.password.trim() |
||||
Accounts.setPassword userData._id, userData.password.trim() |
||||
|
||||
return true |
@ -1,30 +0,0 @@ |
||||
Meteor.methods |
||||
updateUser: (userData) -> |
||||
if not Meteor.userId() |
||||
throw new Meteor.Error('invalid-user', "[methods] updateUser -> Invalid user") |
||||
|
||||
user = Meteor.user() |
||||
|
||||
canEditUserPermission = RocketChat.authz.hasPermission( user._id, 'edit-other-user-info') |
||||
if user._id isnt userData._id and canEditUserPermission isnt true |
||||
throw new Meteor.Error 'not-authorized', '[methods] updateUser -> Not authorized' |
||||
|
||||
unless userData._id |
||||
throw new Meteor.Error 'id-is-required', '[methods] updateUser -> User id is required' |
||||
|
||||
unless userData.name |
||||
throw new Meteor.Error 'name-is-required', 'Name field is required' |
||||
|
||||
unless userData.username |
||||
throw new Meteor.Error 'user-name-is-required', 'Username field is required' |
||||
|
||||
Meteor.users.update { _id: userData._id }, { $set: { name: userData.name } } |
||||
|
||||
Meteor.runAsUser userData._id, -> |
||||
Meteor.call 'setUsername', userData.username |
||||
|
||||
canEditUserPassword = RocketChat.authz.hasPermission( user._id, 'edit-other-user-password') |
||||
if canEditUserPassword and userData.password.trim() |
||||
Accounts.setPassword userData._id, userData.password.trim() |
||||
|
||||
return true |
@ -0,0 +1,19 @@ |
||||
<template name="requestPasswordChange"> |
||||
<div class="content"> |
||||
<div class="attention-message"> |
||||
<i class="icon-attention"></i> |
||||
<span>{{_ 'You_need_to_change_your_password'}}</span> |
||||
</div> |
||||
<div class="rocket-form request-password"> |
||||
<form> |
||||
<fieldset> |
||||
<label for="oldPassword">{{_ "Old_Password"}}</label><input type="password" name="oldPassword" id="oldPassword" /> |
||||
<label for="newPassword">{{_ "Password"}}</label><input type="password" name="newPassword" id="newPassword" /> |
||||
<div class="submit"> |
||||
<button type="submit" class="button save"><i class="icon-send"></i><span>{{_ "Save"}}</span></button> |
||||
</div> |
||||
</fieldset> |
||||
</form> |
||||
</div> |
||||
</div> |
||||
</template> |
@ -0,0 +1,31 @@ |
||||
Template.requestPasswordChange.events({ |
||||
'submit'(e, instance) { |
||||
e.preventDefault(); |
||||
oldPassword = s.trim(instance.$('#oldPassword').val()); |
||||
newPassword = s.trim(instance.$('#newPassword').val()); |
||||
instance.changePassword(oldPassword, newPassword); |
||||
} |
||||
}) |
||||
|
||||
Template.requestPasswordChange.onCreated(function() { |
||||
this.changePassword = function(oldPassword, newPassword) { |
||||
if (!oldPassword || !newPassword) { |
||||
toastr.warning(t('Old_and_new_password_required')); |
||||
} else { |
||||
Accounts.changePassword(oldPassword, newPassword, function(error) { |
||||
if(error) { |
||||
toastr.error(t('Incorrect_Password')); |
||||
} else { |
||||
Meteor.call('clearRequestPasswordChange', function() { |
||||
toastr.success(t('Password_changed_successfully')) |
||||
return true; |
||||
}); |
||||
} |
||||
}); |
||||
} |
||||
} |
||||
}) |
||||
|
||||
Template.requestPasswordChange.onRendered(function() { |
||||
this.$('#oldPassword').focus(); |
||||
}) |
Loading…
Reference in new issue