// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved. // See License.txt for license information. var SettingItemMin = require('./setting_item_min.jsx'); var SettingItemMax = require('./setting_item_max.jsx'); var client = require('../utils/client.jsx'); var AsyncClient = require('../utils/async_client.jsx'); var Constants = require('../utils/constants.jsx'); module.exports = React.createClass({ displayName: 'SecurityTab', submitPassword: function(e) { e.preventDefault(); var user = this.props.user; var currentPassword = this.state.currentPassword; var newPassword = this.state.newPassword; var confirmPassword = this.state.confirmPassword; if (currentPassword === '') { this.setState({passwordError: 'Please enter your current password', serverError: ''}); return; } if (newPassword.length < 5) { this.setState({passwordError: 'New passwords must be at least 5 characters', serverError: ''}); return; } if (newPassword !== confirmPassword) { this.setState({passwordError: 'The new passwords you entered do not match', serverError: ''}); return; } var data = {}; data.user_id = user.id; data.current_password = currentPassword; data.new_password = newPassword; client.updatePassword(data, function() { this.props.updateSection(''); AsyncClient.getMe(); this.setState({currentPassword: '', newPassword: '', confirmPassword: ''}); }.bind(this), function(err) { var state = this.getInitialState(); if (err.message) { state.serverError = err.message; } else { state.serverError = err; } state.passwordError = ''; this.setState(state); }.bind(this) ); }, updateCurrentPassword: function(e) { this.setState({currentPassword: e.target.value}); }, updateNewPassword: function(e) { this.setState({newPassword: e.target.value}); }, updateConfirmPassword: function(e) { this.setState({confirmPassword: e.target.value}); }, handleHistoryOpen: function() { $("#user_settings").modal('hide'); }, handleDevicesOpen: function() { $("#user_settings").modal('hide'); }, handleClose: function() { $(this.getDOMNode()).find('.form-control').each(function() { this.value = ''; }); this.setState({currentPassword: '', newPassword: '', confirmPassword: '', serverError: null, passwordError: null}); this.props.updateTab('general'); }, componentDidMount: function() { $('#user_settings').on('hidden.bs.modal', this.handleClose); }, componentWillUnmount: function() { $('#user_settings').off('hidden.bs.modal', this.handleClose); this.props.updateSection(''); }, getInitialState: function() { return {currentPassword: '', newPassword: '', confirmPassword: ''}; }, render: function() { var serverError = this.state.serverError ? this.state.serverError : null; var passwordError = this.state.passwordError ? this.state.passwordError : null; var updateSectionStatus; var passwordSection; var self = this; if (this.props.activeSection === 'password') { var inputs = []; var submit = null; if (this.props.user.auth_service === '') { inputs.push(
); inputs.push(
); inputs.push(
); submit = this.submitPassword; } else { inputs.push(
); } updateSectionStatus = function(e) { self.props.updateSection(''); self.setState({currentPassword: '', newPassword: '', confirmPassword: '', serverError: null, passwordError: null}); e.preventDefault(); }; passwordSection = ( ); } else { var describe; if (this.props.user.auth_service === '') { var d = new Date(this.props.user.last_password_update); var hour = d.getHours() % 12 ? String(d.getHours() % 12) : '12'; var min = d.getMinutes() < 10 ? '0' + d.getMinutes() : String(d.getMinutes()); var timeOfDay = d.getHours() >= 12 ? ' pm' : ' am'; describe = 'Last updated ' + Constants.MONTHS[d.getMonth()] + ' ' + d.getDate() + ', ' + d.getFullYear() + ' at ' + hour + ':' + min + timeOfDay; } else { describe = 'Log in done through GitLab'; } updateSectionStatus = function() { self.props.updateSection('password'); }; passwordSection = ( ); } return (

Security Settings

Security Settings

{passwordSection}
); } });