Load settings to redux and allow login via LDAP and CROWD

This commit is contained in:
Rodrigo Nascimento 2017-08-13 20:02:46 -03:00
parent abd6d01b54
commit 10652002bc
12 changed files with 130 additions and 90 deletions

View File

@ -7,9 +7,13 @@ export function setCurrentServer(server) {
}; };
} }
export function preventESLintError() { export function setAllSettings(settings) {
return {}; return {
type: types.SET_ALL_SETTINGS,
payload: settings
};
} }
// // GENRES // // GENRES
// export function retrieveMoviesGenresSuccess(res) { // export function retrieveMoviesGenresSuccess(res) {
// return { // return {

15
app/constants/settings.js Normal file
View File

@ -0,0 +1,15 @@
export default {
boolean: 'valueAsBoolean',
int: 'valueAsNumber',
string: 'valueAsString',
select: 'valueAsString',
code: 'valueAsString',
relativeUrl: 'valueAsString',
language: 'valueAsString',
action: 'valueAsString',
password: 'valueAsString',
// asset: ' Object',
color: 'valueAsString',
font: 'valueAsString',
roomPick: 'valueAsString'
};

View File

@ -7,4 +7,4 @@
// export const RETRIEVE_MOVIES_SEARCH_RESULT_SUCCESS = 'RETRIEVE_MOVIES_SEARCH_RESULT_SUCCESS'; // export const RETRIEVE_MOVIES_SEARCH_RESULT_SUCCESS = 'RETRIEVE_MOVIES_SEARCH_RESULT_SUCCESS';
export const SET_CURRENT_SERVER = 'SET_CURRENT_SERVER'; export const SET_CURRENT_SERVER = 'SET_CURRENT_SERVER';
export const ESLINT_FIX = 'ESLINT_FIX'; export const SET_ALL_SETTINGS = 'SET_ALL_SETTINGS';

View File

@ -13,10 +13,8 @@ if (__DEV__) {
middleware = [...middleware]; middleware = [...middleware];
} }
export default function configureStore(initialState) { export default createStore(
return createStore( rootReducer,
rootReducer, undefined,
initialState, applyMiddleware(...middleware)
applyMiddleware(...middleware) );
);
}

View File

@ -15,7 +15,9 @@ const settingsSchema = {
properties: { properties: {
_id: 'string', _id: 'string',
_server: 'servers', _server: 'servers',
value: { type: 'string', optional: true } valueAsString: { type: 'string', optional: true },
valueAsBoolean: { type: 'bool', optional: true },
valueAsNumber: { type: 'int', optional: true }
} }
}; };
@ -70,13 +72,14 @@ const messagesSchema = {
} }
}; };
// Realm.clearTestState();
const realm = new Realm({ const realm = new Realm({
schema: [settingsSchema, serversSchema, subscriptionSchema, messagesSchema, usersSchema] schema: [settingsSchema, serversSchema, subscriptionSchema, messagesSchema, usersSchema]
}); });
export default realm; export default realm;
// Realm.clearTestState();
// realm.write(() => { // realm.write(() => {
// realm.create('servers', { id: 'https://demo.rocket.chat', current: false }, true); // realm.create('servers', { id: 'https://demo.rocket.chat', current: false }, true);
// realm.create('servers', { id: 'http://localhost:3000', current: false }, true); // realm.create('servers', { id: 'http://localhost:3000', current: false }, true);

View File

@ -1,9 +1,13 @@
import Meteor from 'react-native-meteor'; import Meteor from 'react-native-meteor';
import Random from 'react-native-meteor/lib/Random'; import Random from 'react-native-meteor/lib/Random';
import { AsyncStorage } from 'react-native'; import { AsyncStorage } from 'react-native';
import { hashPassword } from 'react-native-meteor/lib/utils';
import reduxStore from '../lib/createStore';
import settingsType from '../constants/settings';
import realm from './realm'; import realm from './realm';
import debounce from '../utils/debounce'; import debounce from '../utils/debounce';
import * as actions from '../actions';
export { Accounts } from 'react-native-meteor'; export { Accounts } from 'react-native-meteor';
@ -76,19 +80,22 @@ const RocketChat = {
console.error(err); console.error(err);
} }
const settings = {};
realm.write(() => { realm.write(() => {
data.forEach((item) => { data.forEach((item) => {
const setting = { const setting = {
_id: item._id _id: item._id
}; };
setting._server = { id: RocketChat.currentServer }; setting._server = { id: RocketChat.currentServer };
if (typeof item.value === 'string') { if (settingsType[item.type]) {
setting.value = item.value; setting[settingsType[item.type]] = item.value;
realm.create('settings', setting, true);
} }
// write('settings', setting);
realm.create('settings', setting, true); settings[item._id] = item.value;
}); });
}); });
reduxStore.dispatch(actions.setAllSettings(settings));
if (cb) { if (cb) {
cb(); cb();
@ -138,8 +145,53 @@ const RocketChat = {
}); });
}, },
loginWithPassword(selector, password, cb) { login(params, callback) {
Meteor.loginWithPassword(selector, password, () => cb && cb()); Meteor._startLoggingIn();
Meteor.call('login', params, (err, result) => {
Meteor._endLoggingIn();
Meteor._handleLoginCallback(err, result);
if (typeof callback === 'function') {
callback(err);
}
});
},
loginWithPassword(username, password, callback) {
let params = {};
const state = reduxStore.getState();
if (state.settings.LDAP_Enable) {
params = {
ldap: true,
username,
ldapPass: password,
ldapOptions: {}
};
} else if (state.settings.CROWD_Enable) {
params = {
crowd: true,
username,
crowdPassword: password
};
} else {
params = {
password: hashPassword(password),
user: {
username
}
};
if (typeof username === 'string') {
if (username.indexOf('@') !== -1) {
params.user = { email: username };
}
}
}
console.log({ params });
this.login(params, callback);
}, },
loadSubscriptions(cb) { loadSubscriptions(cb) {
@ -333,6 +385,10 @@ const RocketChat = {
export default RocketChat; export default RocketChat;
if (RocketChat.currentServer) {
reduxStore.dispatch(actions.setCurrentServer(RocketChat.currentServer));
}
Meteor.Accounts.onLogin(() => { Meteor.Accounts.onLogin(() => {
Promise.all([call('subscriptions/get'), call('rooms/get')]).then(([subscriptions, rooms]) => { Promise.all([call('subscriptions/get'), call('rooms/get')]).then(([subscriptions, rooms]) => {
subscriptions = subscriptions.sort((s1, s2) => (s1.rid > s2.rid ? 1 : -1)); subscriptions = subscriptions.sort((s1, s2) => (s1.rid > s2.rid ? 1 : -1));

View File

@ -7,9 +7,7 @@ import ListServerView from './views/serverList';
import RoomsListView from './views/roomsList'; import RoomsListView from './views/roomsList';
import RoomView from './views/room'; import RoomView from './views/room';
import CreateChannel from './views/CreateChannel'; import CreateChannel from './views/CreateChannel';
import configureStore from './lib/createStore'; import store from './lib/createStore';
const store = configureStore();
Navigation.registerComponent('Rooms', () => RoomsListView, store, Provider); Navigation.registerComponent('Rooms', () => RoomsListView, store, Provider);
Navigation.registerComponent('Room', () => RoomView, store, Provider); Navigation.registerComponent('Room', () => RoomView, store, Provider);

View File

@ -1,6 +1,5 @@
import RocketChat from '../lib/rocketchat';
export default { export default {
server: RocketChat.currentServer, server: null,
login: {} login: {},
settings: {}
}; };

View File

@ -2,51 +2,21 @@ import RocketChat from '../lib/rocketchat';
import * as types from '../constants/types'; import * as types from '../constants/types';
import initialState from './initialState'; import initialState from './initialState';
export default function(state = initialState, action) { export function server(state = initialState.server, action) {
switch (action.type) { if (action.type === types.SET_CURRENT_SERVER) {
case types.SET_CURRENT_SERVER: RocketChat.currentServer = action.payload;
RocketChat.currentServer = action.payload; return action.payload;
return {
...state,
server: action.payload
};
// case types.RETRIEVE_POPULAR_MOVIES_SUCCESS:
// return {
// ...state,
// popularMovies: action.popularMovies
// };
// case types.RETRIEVE_NOWPLAYING_MOVIES_SUCCESS:
// return {
// ...state,
// nowPlayingMovies: action.nowPlayingMovies
// };
// case types.RETRIEVE_MOVIES_GENRES_SUCCESS:
// return {
// ...state,
// genres: action.moviesGenres
// };
// case types.RETRIEVE_MOVIES_LIST_SUCCESS:
// return {
// ...state,
// list: action.list
// };
// case types.RETRIEVE_MOVIE_DETAILS_SUCCESS:
// return {
// ...state,
// details: action.details
// };
// case types.RETRIEVE_MOVIES_SEARCH_RESULT_SUCCESS:
// return {
// ...state,
// searchResults: action.searchResults
// };
default:
return state;
} }
return state;
}
export function settings(state = initialState.settings, action) {
if (action.type === types.SET_ALL_SETTINGS) {
return {
...action.payload
};
}
return state;
} }

View File

@ -1,9 +1,8 @@
// import { combineReducers } from 'redux'; import { combineReducers } from 'redux';
import reducers from './reducers'; import * as reducers from './reducers';
// const rootReducer = combineReducers({ const rootReducer = combineReducers({
// reducers ...reducers
// }); });
// export default rootReducer; export default rootReducer;
export default reducers;

View File

@ -1,7 +1,7 @@
import ActionButton from 'react-native-action-button'; import ActionButton from 'react-native-action-button';
import Icon from 'react-native-vector-icons/Ionicons'; import Icon from 'react-native-vector-icons/Ionicons';
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; // import PropTypes from 'prop-types';
import { TextInput, StyleSheet, View, Text, Switch } from 'react-native'; import { TextInput, StyleSheet, View, Text, Switch } from 'react-native';
import RocketChat from '../lib/rocketchat'; import RocketChat from '../lib/rocketchat';
@ -45,9 +45,9 @@ const styles = StyleSheet.create({
}); });
const mainIcon = <Icon name='md-checkmark' style={styles.actionButtonIcon} />; const mainIcon = <Icon name='md-checkmark' style={styles.actionButtonIcon} />;
export default class CreateChannelView extends React.Component { export default class CreateChannelView extends React.Component {
static propTypes = { // static propTypes = {
navigation: PropTypes.object.isRequired // navigation: PropTypes.object.isRequired
} // }
static navigationOptions = () => ({ static navigationOptions = () => ({
title: 'Create Channel' title: 'Create Channel'

View File

@ -100,23 +100,22 @@ class RoomsListItem extends React.PureComponent {
} }
@connect(state => ({ @connect(state => ({
server: state.server server: state.server,
Site_Url: state.settings.Site_Url
}), dispatch => ({ }), dispatch => ({
actions: bindActionCreators(actions, dispatch) actions: bindActionCreators(actions, dispatch)
})) }))
export default class RoomsListView extends React.Component { export default class RoomsListView extends React.Component {
static propTypes = { static propTypes = {
navigator: PropTypes.object.isRequired, navigator: PropTypes.object.isRequired,
server: PropTypes.string server: PropTypes.string,
Site_Url: PropTypes.string
} }
constructor(props) { constructor(props) {
super(props); super(props);
this.data = realm.objects('subscriptions').filtered('_server.id = $0', this.props.server); this.data = realm.objects('subscriptions').filtered('_server.id = $0', this.props.server);
const siteUrl = realm.objectForPrimaryKey('settings', 'Site_Url');
if (siteUrl) {
this.url = siteUrl.value;
}
this.state = { this.state = {
dataSource: ds.cloneWithRows([]), dataSource: ds.cloneWithRows([]),
searching: false, searching: false,
@ -249,7 +248,6 @@ export default class RoomsListView extends React.Component {
getSubscriptions = () => this.data.sorted('_updatedAt', true) getSubscriptions = () => this.data.sorted('_updatedAt', true)
updateState = debounce(() => { updateState = debounce(() => {
this.url = realm.objectForPrimaryKey('settings', 'Site_Url').value;
this.setState({ this.setState({
dataSource: ds.cloneWithRows(this.data.filtered('_server.id = $0', this.props.server).sorted('ls', true)) dataSource: ds.cloneWithRows(this.data.filtered('_server.id = $0', this.props.server).sorted('ls', true))
}); });
@ -319,7 +317,7 @@ export default class RoomsListView extends React.Component {
<RoomsListItem <RoomsListItem
item={item} item={item}
onPress={() => this._onPressItem(item._id, item)} onPress={() => this._onPressItem(item._id, item)}
baseUrl={this.url} baseUrl={this.props.Site_Url}
/> />
); );