Skip to content

Commit

Permalink
Replace all Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigok committed Sep 22, 2015
1 parent 9e929f2 commit aae6806
Show file tree
Hide file tree
Showing 16 changed files with 176 additions and 98 deletions.
22 changes: 11 additions & 11 deletions packages/meteor-accounts-saml/saml_rocketchat.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ updateServices = ->
Meteor.clearTimeout timer if timer?

timer = Meteor.setTimeout ->
services = Settings.find({_id: /^(SAML_Custom_)[a-z]+$/i}).fetch()
services = RocketChat.models.Settings.find({_id: /^(SAML_Custom_)[a-z]+$/i}).fetch()

Accounts.saml.settings.providers = []

Expand All @@ -27,26 +27,26 @@ updateServices = ->

if service.value is true
data =
buttonLabelText: Settings.findOne({_id: "#{service._id}_button_label_text"})?.value
buttonLabelColor: Settings.findOne({_id: "#{service._id}_button_label_color"})?.value
buttonColor: Settings.findOne({_id: "#{service._id}_button_color"})?.value
buttonLabelText: RocketChat.models.Settings.findOneById("#{service._id}_button_label_text")?.value
buttonLabelColor: RocketChat.models.Settings.findOneById("#{service._id}_button_label_color")?.value
buttonColor: RocketChat.models.Settings.findOneById("#{service._id}_button_color")?.value
clientConfig:
provider: Settings.findOne({_id: "#{service._id}_provider"})?.value
provider: RocketChat.models.Settings.findOneById("#{service._id}_provider")?.value

Accounts.saml.settings.generateUsername = Settings.findOne({_id: "#{service._id}_generate_username"})?.value
Accounts.saml.settings.generateUsername = RocketChat.models.Settings.findOneById("#{service._id}_generate_username")?.value

Accounts.saml.settings.providers.push
provider: data.clientConfig.provider
entryPoint: Settings.findOne({_id: "#{service._id}_entry_point"})?.value
issuer: Settings.findOne({_id: "#{service._id}_issuer"})?.value
cert: Settings.findOne({_id: "#{service._id}_cert"})?.value
entryPoint: RocketChat.models.Settings.findOneById("#{service._id}_entry_point")?.value
issuer: RocketChat.models.Settings.findOneById("#{service._id}_issuer")?.value
cert: RocketChat.models.Settings.findOneById("#{service._id}_cert")?.value

ServiceConfiguration.configurations.upsert {service: serviceName.toLowerCase()}, $set: data
else
ServiceConfiguration.configurations.remove {service: serviceName.toLowerCase()}
, 2000

Settings.find().observe
RocketChat.models.Settings.find().observe
added: (record) ->
if /^SAML_.+/.test record._id
updateServices()
Expand All @@ -60,5 +60,5 @@ Settings.find().observe
updateServices()

Meteor.startup ->
if not Settings.findOne({_id: /^(SAML_Custom)[a-z]+$/i})?
if not RocketChat.models.Settings.findOne({_id: /^(SAML_Custom)[a-z]+$/i})?
Meteor.call 'addSamlService', 'default'
4 changes: 2 additions & 2 deletions packages/rocketchat-ldap/config_server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ updateServices = ->
Meteor.clearTimeout timer if timer?

timer = Meteor.setTimeout ->
enable = Settings.findOne({_id: 'LDAP_Enable', value: true})
enable = RocketChat.models.Settings.findOne({_id: 'LDAP_Enable', value: true})

if enable?
console.log "Enabling LDAP".blue
Expand All @@ -29,7 +29,7 @@ updateServices = ->
LDAP_DEFAULTS.bindSearch = undefined
, 2000

Settings.find().observe
RocketChat.models.Settings.find().observe
added: (record) ->
if /^LDAP_.+/.test record._id
updateServices()
Expand Down
5 changes: 3 additions & 2 deletions packages/rocketchat-ldap/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Package.describe({
});

Npm.depends({
ldapjs: "0.7.1",
ldapjs: "0.7.1",
});

// Loads all i18n.json files into tapi18nFiles
Expand All @@ -22,6 +22,7 @@ Package.onUse(function(api) {
api.versionsFrom('1.0.3.1');

// Commom
api.use('rocketchat:[email protected]');
api.use('tap:[email protected]');
api.use('yasaricli:slugify');
api.use('coffeescript');
Expand All @@ -43,7 +44,7 @@ Package.onUse(function(api) {
api.addFiles('config_server.coffee', 'server');

api.addFiles(tapi18nFiles);

api.export('LDAP', 'server');
api.export('LDAP_DEFAULTS', 'server');
api.export('MeteorWrapperLdapjs');
Expand Down
36 changes: 21 additions & 15 deletions packages/rocketchat-lib/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Package.onUse(function(api) {
api.use('check');
api.use('underscore');
api.use('underscorestring:underscore.string');
api.use('monbro:[email protected]');

// TAPi18n
api.use('templating', 'client');
Expand All @@ -29,14 +30,33 @@ Package.onUse(function(api) {
api.imply('tap:i18n');
api.addFiles("package-tap.i18n", ["client", "server"]);


// COMMON
api.addFiles('lib/core.coffee');
api.addFiles('lib/callbacks.coffee');
api.addFiles('lib/slashCommand.coffee');

api.addFiles('settings/lib/settings.coffee');
// MODELS SERVER
api.addFiles('server/models/_Base.coffee', 'server');
api.addFiles('server/models/Users.coffee', 'server');
api.addFiles('server/models/Subscriptions.coffee', 'server');
api.addFiles('server/models/Rooms.coffee', 'server');
api.addFiles('server/models/Messages.coffee', 'server');
api.addFiles('server/models/Reports.coffee', 'server');

// Settings
api.addFiles('settings/lib/rocketchat.coffee');

api.addFiles('settings/server/models/Settings.coffee', 'server');
api.addFiles('settings/server/methods.coffee', 'server');
api.addFiles('settings/server/publication.coffee', 'server');
api.addFiles('settings/server/startup.coffee', 'server');
api.addFiles('settings/server/updateServices.coffee', 'server');
api.addFiles('settings/server/addOAuthService.coffee', 'server');

api.addFiles('settings/lib/settings.coffee');


// CLIENT
api.addFiles('client/Notifications.coffee', 'client');
api.addFiles('client/TabBar.coffee', 'client');
Expand All @@ -59,22 +79,8 @@ Package.onUse(function(api) {

api.addFiles('server/Notifications.coffee', 'server');

// Settings
api.addFiles('settings/server/methods.coffee', 'server');
api.addFiles('settings/server/publication.coffee', 'server');
api.addFiles('settings/server/startup.coffee', 'server');
api.addFiles('settings/server/updateServices.coffee', 'server');
api.addFiles('settings/server/addOAuthService.coffee', 'server');

api.addFiles('server/cdn.coffee', 'server');

api.addFiles('server/models/_Base.coffee', 'server');
api.addFiles('server/models/Users.coffee', 'server');
api.addFiles('server/models/Subscriptions.coffee', 'server');
api.addFiles('server/models/Rooms.coffee', 'server');
api.addFiles('server/models/Messages.coffee', 'server');
api.addFiles('server/models/Reports.coffee', 'server');

// TAPi18n -- needs to be added last
api.addFiles(tapi18nFiles, ["client", "server"]);

Expand Down
5 changes: 4 additions & 1 deletion packages/rocketchat-lib/settings/lib/settings.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
@Settings = new Meteor.Collection 'rocketchat_settings'
if Meteor.isClient is true
@Settings = Settings = new Meteor.Collection 'rocketchat_settings'
else
Settings = RocketChat.models.Settings

Settings.find().observe
added: (record) ->
Expand Down
20 changes: 10 additions & 10 deletions packages/rocketchat-lib/settings/server/addOAuthService.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ Meteor.methods
throw new Meteor.Error 'not-authorized', '[methods] addOAuthService -> Not authorized'

name = s.capitalize(name)
Settings.remove _id: "Accounts_OAuth_Custom_#{name}"
Settings.remove _id: "Accounts_OAuth_Custom_#{name}_url"
Settings.remove _id: "Accounts_OAuth_Custom_#{name}_token_path"
Settings.remove _id: "Accounts_OAuth_Custom_#{name}_identity_path"
Settings.remove _id: "Accounts_OAuth_Custom_#{name}_authorize_path"
Settings.remove _id: "Accounts_OAuth_Custom_#{name}_id"
Settings.remove _id: "Accounts_OAuth_Custom_#{name}_secret"
Settings.remove _id: "Accounts_OAuth_Custom_#{name}_button_label_text"
Settings.remove _id: "Accounts_OAuth_Custom_#{name}_button_label_color"
Settings.remove _id: "Accounts_OAuth_Custom_#{name}_button_color"
RocketChat.settings.removeById "Accounts_OAuth_Custom_#{name}"
RocketChat.settings.removeById "Accounts_OAuth_Custom_#{name}_url"
RocketChat.settings.removeById "Accounts_OAuth_Custom_#{name}_token_path"
RocketChat.settings.removeById "Accounts_OAuth_Custom_#{name}_identity_path"
RocketChat.settings.removeById "Accounts_OAuth_Custom_#{name}_authorize_path"
RocketChat.settings.removeById "Accounts_OAuth_Custom_#{name}_id"
RocketChat.settings.removeById "Accounts_OAuth_Custom_#{name}_secret"
RocketChat.settings.removeById "Accounts_OAuth_Custom_#{name}_button_label_text"
RocketChat.settings.removeById "Accounts_OAuth_Custom_#{name}_button_label_color"
RocketChat.settings.removeById "Accounts_OAuth_Custom_#{name}_button_color"
31 changes: 23 additions & 8 deletions packages/rocketchat-lib/settings/server/methods.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
###
# Add a setting
# Add a setting
# @param {String} _id
# @param {Mixed} value
# @param {Object} setting
Expand All @@ -24,7 +24,7 @@ RocketChat.settings.add = (_id, value, options = {}) ->
updateSettings.section = options.section if options.section
updateSettings.public = options.public if options.public

return Settings.upsert { _id: _id }, { $setOnInsert: { value: value }, $set: updateSettings }
return RocketChat.models.Settings.upsert { _id: _id }, { $setOnInsert: { value: value }, $set: updateSettings }

###
# Add a setting group
Expand All @@ -37,22 +37,37 @@ RocketChat.settings.addGroup = (_id, options = {}) ->

# console.log '[functions] RocketChat.settings.addGroup -> '.green, 'arguments:', arguments

updateSettings =
updateSettings =
i18nLabel: options.i18nLabel or _id
i18nDescription: options.i18nDescription if options.i18nDescription?
type: 'group'

return Settings.upsert { _id: _id }, { $set: updateSettings }

return RocketChat.models.Settings.upsert { _id: _id }, { $set: updateSettings }


###
# Remove a setting by id
# @param {String} _id
###

RocketChat.settings.removeById = (_id) ->
if not _id
return false

# console.log '[functions] RocketChat.settings.add -> '.green, 'arguments:', arguments

return RocketChat.models.Settings.removeById _id


Meteor.methods
saveSetting: (_id, value) ->
console.log '[method] saveSetting', _id, value
if Meteor.userId()?
user = Meteor.users.findOne Meteor.userId()

unless RocketChat.authz.hasPermission(Meteor.userId(), 'edit-privileged-setting') is true
throw new Meteor.Error 503, 'Not authorized'

# console.log "saveSetting -> ".green, _id, value
Settings.update { _id: _id }, { $set: { value: value } }
return true
RocketChat.models.Settings.updateValueById _id, value
return true
54 changes: 54 additions & 0 deletions packages/rocketchat-lib/settings/server/models/Settings.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
RocketChat.models.Settings = new class extends RocketChat.models._Base
constructor: ->
@_initModel 'settings'


# FIND ONE
findOneById: (_id, options) ->
query =
_id: _id

return @findOne query, options


# FIND
findByRole: (role, options) ->
query =
role: role

return @find query, options

findPublic: (options) ->
query =
public: true

return @find query, options


# UPDATE
updateValueById: (_id, value) ->
query =
_id: _id

update =
$set:
value: value

return @update query, update


# REMOVE
createWithIdAndValue: (_id, value) ->
record =
_id: _id
value: value

return @insert record


# REMOVE
removeById: (_id) ->
query =
_id: _id

return @remove query
4 changes: 2 additions & 2 deletions packages/rocketchat-lib/settings/server/publication.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Meteor.publish 'settings', ->
console.log '[publish] settings'.green
return Settings.find { public: true }, { fields: _id: 1, value: 1 }
return RocketChat.models.Settings.findPublic { fields: _id: 1, value: 1 }

Meteor.publish 'admin-settings', ->
console.log '[publish] admin-settings'.green
Expand All @@ -9,7 +9,7 @@ Meteor.publish 'admin-settings', ->
return @ready()

if RocketChat.authz.hasPermission( @userId, 'view-privileged-setting')
return Settings.find()
return RocketChat.models.Settings.find()
else
return @ready()

6 changes: 5 additions & 1 deletion packages/rocketchat-lib/settings/server/startup.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
Meteor.startup ->
# Insert server unique id if it doesn't exist
if not RocketChat.models.Settings.findOneById 'uniqueID'
RocketChat.models.Settings.createWithIdAndValue 'uniqueID', Random.id()

RocketChat.settings.addGroup 'Accounts'
RocketChat.settings.add 'Accounts_RegistrationRequired', true, { type: 'boolean', group: 'Accounts', public: true, section: 'Registration' }
RocketChat.settings.add 'Accounts_EmailVerification', false, { type: 'boolean', group: 'Accounts', public: true, section: 'Registration' }
Expand Down Expand Up @@ -45,7 +49,7 @@ Meteor.startup ->
RocketChat.settings.add 'SMTP_Username', '', { type: 'string', group: 'SMTP' }
RocketChat.settings.add 'SMTP_Password', '', { type: 'string', group: 'SMTP' }
RocketChat.settings.add 'From_Email', '[email protected]', { type: 'string', group: 'SMTP' }

RocketChat.settings.add 'Invitation_Subject', 'You have been invited to Rocket.Chat', { type: 'string', group: 'SMTP', section: 'Invitation' }
RocketChat.settings.add 'Invitation_HTML', '<h2>You have been invited to <h1>Rocket.Chat</h1></h2><p>Go to https://demo.rocket.chat and try the best open source chat solution available today!</p>', { type: 'string', multiline: true, group: 'SMTP', section: 'Invitation' }

Expand Down
22 changes: 11 additions & 11 deletions packages/rocketchat-lib/settings/server/updateServices.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ updateServices = ->
Meteor.clearTimeout timer if timer?

timer = Meteor.setTimeout ->
services = Settings.find({_id: /^(Accounts_OAuth_|Accounts_OAuth_Custom_)[a-z]+$/i}).fetch()
services = RocketChat.models.Settings.find({_id: /^(Accounts_OAuth_|Accounts_OAuth_Custom_)[a-z]+$/i}).fetch()

for service in services
console.log "Updating login service #{service._id}".blue
Expand All @@ -18,18 +18,18 @@ updateServices = ->

if service.value is true
data =
clientId: Settings.findOne({_id: "#{service._id}_id"})?.value
secret: Settings.findOne({_id: "#{service._id}_secret"})?.value
clientId: RocketChat.models.Settings.findOneById("#{service._id}_id")?.value
secret: RocketChat.models.Settings.findOneById("#{service._id}_secret")?.value

if /Accounts_OAuth_Custom_/.test service._id
data.custom = true
data.serverURL = Settings.findOne({_id: "#{service._id}_url"})?.value
data.tokenPath = Settings.findOne({_id: "#{service._id}_token_path"})?.value
data.identityPath = Settings.findOne({_id: "#{service._id}_identity_path"})?.value
data.authorizePath = Settings.findOne({_id: "#{service._id}_authorize_path"})?.value
data.buttonLabelText = Settings.findOne({_id: "#{service._id}_button_label_text"})?.value
data.buttonLabelColor = Settings.findOne({_id: "#{service._id}_button_label_color"})?.value
data.buttonColor = Settings.findOne({_id: "#{service._id}_button_color"})?.value
data.serverURL = RocketChat.models.Settings.findOneById("#{service._id}_url")?.value
data.tokenPath = RocketChat.models.Settings.findOneById("#{service._id}_token_path")?.value
data.identityPath = RocketChat.models.Settings.findOneById("#{service._id}_identity_path")?.value
data.authorizePath = RocketChat.models.Settings.findOneById("#{service._id}_authorize_path")?.value
data.buttonLabelText = RocketChat.models.Settings.findOneById("#{service._id}_button_label_text")?.value
data.buttonLabelColor = RocketChat.models.Settings.findOneById("#{service._id}_button_label_color")?.value
data.buttonColor = RocketChat.models.Settings.findOneById("#{service._id}_button_color")?.value
new CustomOAuth serviceName.toLowerCase(),
serverURL: data.serverURL
tokenPath: data.tokenPath
Expand All @@ -49,7 +49,7 @@ updateServices = ->
ServiceConfiguration.configurations.remove {service: serviceName.toLowerCase()}
, 2000

Settings.find().observe
RocketChat.models.Settings.find().observe
added: (record) ->
if /^Accounts_OAuth_.+/.test record._id
updateServices()
Expand Down
Loading

0 comments on commit aae6806

Please sign in to comment.