From 5f8108d8ff2eebf700879def198648562ea1445e Mon Sep 17 00:00:00 2001 From: Djorkaeff Alexandre Date: Thu, 20 Feb 2020 17:08:20 -0300 Subject: [PATCH] [IMPROVEMENT] Honor Register/Login settings (#1727) Co-authored-by: Diego Mello --- app/constants/settings.js | 9 ++++ app/lib/methods/getSettings.js | 17 +++++++ app/lib/rocketchat.js | 5 +- app/sagas/selectServer.js | 3 +- app/views/LoginSignupView.js | 88 +++++++++++++++++++++++++--------- app/views/LoginView.js | 39 ++++++++++----- 6 files changed, 124 insertions(+), 37 deletions(-) diff --git a/app/constants/settings.js b/app/constants/settings.js index 4b54b400..4f2c1f44 100644 --- a/app/constants/settings.js +++ b/app/constants/settings.js @@ -35,6 +35,15 @@ export default { Accounts_PasswordReset: { type: 'valueAsBoolean' }, + Accounts_RegistrationForm: { + type: 'valueAsString' + }, + Accounts_RegistrationForm_LinkReplacementText: { + type: 'valueAsString' + }, + Accounts_ShowFormLogin: { + type: 'valueAsBoolean' + }, CROWD_Enable: { type: 'valueAsBoolean' }, diff --git a/app/lib/methods/getSettings.js b/app/lib/methods/getSettings.js index 997e3c53..5ab7aa59 100644 --- a/app/lib/methods/getSettings.js +++ b/app/lib/methods/getSettings.js @@ -52,6 +52,23 @@ const serverInfoUpdate = async(serverInfo, iconSetting) => { }); }; +export function getSetting({ server, setting }) { + return new Promise(async(resolve, reject) => { + try { + const result = await fetch(`${ server }/api/v1/settings.public?query={"_id":{"$in":["${ setting }"]}}`).then(response => response.json()); + + if (result.success && result.settings.length) { + const [{ value }] = result.settings; + return resolve(value); + } + } catch (e) { + log(e); + } + + return reject(); + }); +} + export async function setSettings() { const db = database.active; const settingsCollection = db.collections.get('settings'); diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index 4d5de613..0805ed58 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -24,7 +24,7 @@ import subscribeRooms from './methods/subscriptions/rooms'; import protectedFunction from './methods/helpers/protectedFunction'; import readMessages from './methods/readMessages'; -import getSettings, { setSettings } from './methods/getSettings'; +import getSettings, { getSetting, setSettings } from './methods/getSettings'; import getRooms from './methods/getRooms'; import getPermissions from './methods/getPermissions'; @@ -620,6 +620,7 @@ const RocketChat = { cancelUpload, isUploadActive, getSettings, + getSetting, setSettings, getPermissions, getCustomEmojis, @@ -911,7 +912,7 @@ const RocketChat = { let loginServices = []; const loginServicesResult = await fetch(`${ server }/api/v1/settings.oauth`).then(response => response.json()); - if (loginServicesResult.success && loginServicesResult.services.length > 0) { + if (loginServicesResult.success && loginServicesResult.services) { const { services } = loginServicesResult; loginServices = services; diff --git a/app/sagas/selectServer.js b/app/sagas/selectServer.js index 5cfa51ad..c939868f 100644 --- a/app/sagas/selectServer.js +++ b/app/sagas/selectServer.js @@ -129,8 +129,9 @@ const handleServerRequest = function* handleServerRequest({ server, certificate const serverInfo = yield getServerInfo({ server }); if (serverInfo) { + const showFormLogin = yield RocketChat.getSetting({ server, setting: 'Accounts_ShowFormLogin' }); const loginServicesLength = yield RocketChat.getLoginServices(server); - if (loginServicesLength === 0) { + if (loginServicesLength === 0 && showFormLogin) { Navigation.navigate('LoginView'); } else { Navigation.navigate('LoginSignupView'); diff --git a/app/views/LoginSignupView.js b/app/views/LoginSignupView.js index c2711775..d3c5bb28 100644 --- a/app/views/LoginSignupView.js +++ b/app/views/LoginSignupView.js @@ -58,6 +58,11 @@ const styles = StyleSheet.create({ serviceName: { ...sharedStyles.textBold }, + registerDisabled: { + ...sharedStyles.textRegular, + ...sharedStyles.textAlignCenter, + fontSize: 16 + }, servicesTogglerContainer: { flexDirection: 'row', alignItems: 'center', @@ -108,6 +113,9 @@ class LoginSignupView extends React.Component { Gitlab_URL: PropTypes.string, CAS_enabled: PropTypes.bool, CAS_login_url: PropTypes.string, + Accounts_ShowFormLogin: PropTypes.bool, + Accounts_RegistrationForm: PropTypes.string, + Accounts_RegistrationForm_LinkReplacementText: PropTypes.string, theme: PropTypes.string } @@ -124,7 +132,7 @@ class LoginSignupView extends React.Component { shouldComponentUpdate(nextProps, nextState) { const { collapsed, servicesHeight } = this.state; const { - server, Site_Name, services, theme + server, Site_Name, services, Accounts_ShowFormLogin, Accounts_RegistrationForm, Accounts_RegistrationForm_LinkReplacementText, theme } = this.props; if (nextState.collapsed !== collapsed) { return true; @@ -141,6 +149,15 @@ class LoginSignupView extends React.Component { if (nextProps.theme !== theme) { return true; } + if (nextProps.Accounts_ShowFormLogin !== Accounts_ShowFormLogin) { + return true; + } + if (nextProps.Accounts_RegistrationForm !== Accounts_RegistrationForm) { + return true; + } + if (nextProps.Accounts_RegistrationForm_LinkReplacementText !== Accounts_RegistrationForm_LinkReplacementText) { + return true; + } if (!equal(nextProps.services, services)) { return true; } @@ -333,10 +350,12 @@ class LoginSignupView extends React.Component { renderServicesSeparator = () => { const { collapsed } = this.state; - const { services, theme } = this.props; + const { + services, theme, Accounts_ShowFormLogin, Accounts_RegistrationForm + } = this.props; const { length } = Object.values(services); - if (length > 3) { + if (length > 3 && Accounts_ShowFormLogin && Accounts_RegistrationForm) { return ( @@ -358,6 +377,7 @@ class LoginSignupView extends React.Component { let { name } = service; name = name === 'meteor-developer' ? 'meteor' : name; const icon = `icon_${ name }`; + const isSaml = service.service === 'saml'; let onPress = () => {}; switch (service.authType) { @@ -383,8 +403,8 @@ class LoginSignupView extends React.Component { name = name.charAt(0).toUpperCase() + name.slice(1); const { CAS_enabled, theme } = this.props; let buttonText; - if (service.service === 'saml' || (service.service === 'cas' && CAS_enabled)) { - buttonText = {name}; + if (isSaml || (service.service === 'cas' && CAS_enabled)) { + buttonText = {name}; } else { buttonText = ( <> @@ -396,7 +416,7 @@ class LoginSignupView extends React.Component { @@ -409,15 +429,14 @@ class LoginSignupView extends React.Component { renderServices = () => { const { servicesHeight } = this.state; - const { services } = this.props; + const { services, Accounts_ShowFormLogin, Accounts_RegistrationForm } = this.props; const { length } = Object.values(services); const style = { overflow: 'hidden', height: servicesHeight }; - - if (length > 3) { + if (length > 3 && Accounts_ShowFormLogin && Accounts_RegistrationForm) { return ( {Object.values(services).map(service => this.renderItem(service))} @@ -431,6 +450,38 @@ class LoginSignupView extends React.Component { ); } + renderLogin = () => { + const { Accounts_ShowFormLogin, theme } = this.props; + if (!Accounts_ShowFormLogin) { + return null; + } + return ( +