summaryrefslogtreecommitdiffstats
path: root/web/react/components/team_signup_with_ldap.jsx
blob: 8c808fa8870b06c030a65899e3083fdce624491d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import * as utils from '../utils/utils.jsx';
import * as Client from '../utils/client.jsx';

import {injectIntl, intlShape, defineMessages, FormattedMessage} from 'mm-intl';

const holders = defineMessages({
    team_error: {
        id: 'ldap_signup.team_error',
        defaultMessage: 'Please enter a team name'
    },
    length_error: {
        id: 'ldap_signup.length_error',
        defaultMessage: 'Name must be 3 or more characters up to a maximum of 15'
    },
    teamName: {
        id: 'ldap_signup.teamName',
        defaultMessage: 'Enter name of new team'
    },
    badTeam: {
        id: 'login_ldap.badTeam',
        defaultMessage: 'Bad team name'
    },
    idReq: {
        id: 'login_ldap.idlReq',
        defaultMessage: 'An LDAP ID is required'
    },
    pwdReq: {
        id: 'login_ldap.pwdReq',
        defaultMessage: 'An LDAP password is required'
    },
    username: {
        id: 'login_ldap.username',
        defaultMessage: 'LDAP Username'
    },
    pwd: {
        id: 'login_ldap.pwd',
        defaultMessage: 'LDAP Password'
    }
});

class LdapSignUpPage extends React.Component {
    constructor(props) {
        super(props);

        this.handleSubmit = this.handleSubmit.bind(this);
        this.valueChange = this.valueChange.bind(this);

        this.state = {name: '', id: '', password: ''};
    }

    handleSubmit(e) {
        e.preventDefault();
        const {formatMessage} = this.props.intl;
        var teamSignup = {};
        teamSignup.user = {};
        teamSignup.team = {};
        var state = this.state;
        state.serverError = null;

        teamSignup.team.display_name = this.state.name;

        if (!teamSignup.team.display_name) {
            state.serverError = formatMessage(holders.team_error);
            this.setState(state);
            return;
        }

        if (teamSignup.team.display_name.length <= 2) {
            state.serverError = formatMessage(holders.length_error);
            this.setState(state);
            return;
        }

        const id = this.refs.id.value.trim();
        if (!id) {
            state.serverError = formatMessage(holders.idReq);
            this.setState(state);
            return;
        }

        const password = this.refs.password.value.trim();
        if (!password) {
            state.serverError = formatMessage(holders.pwdReq);
            this.setState(state);
            return;
        }

        state.serverError = '';
        this.setState(state);

        teamSignup.team.name = utils.cleanUpUrlable(teamSignup.team.display_name);
        teamSignup.team.type = 'O';

        teamSignup.user.username = ReactDOM.findDOMNode(this.refs.id).value.trim();
        teamSignup.user.password = ReactDOM.findDOMNode(this.refs.password).value.trim();
        teamSignup.user.allow_marketing = true;
        teamSignup.user.ldap = true;
        teamSignup.user.auth_service = 'ldap';

        $('#ldap-button').button('loading');

        Client.createTeamWithLdap(teamSignup,
            () => {
                Client.track('signup', 'signup_team_ldap_complete');

                Client.loginByLdap(teamSignup.team.name, id, password,
                    () => {
                        window.location.href = '/' + teamSignup.team.name + '/channels/town-square';
                    },
                    (err) => {
                        $('#ldap-button').button('reset');
                        this.setState({serverError: err.message});
                    }
                );
            },
            (err) => {
                $('#ldap-button').button('reset');
                this.setState({serverError: err.message});
            }
        );
    }

    valueChange() {
        this.setState({
            name: ReactDOM.findDOMNode(this.refs.teamname).value.trim(),
            id: ReactDOM.findDOMNode(this.refs.id).value.trim(),
            password: ReactDOM.findDOMNode(this.refs.password).value.trim()
        });
    }

    render() {
        const {formatMessage} = this.props.intl;
        var nameError = null;
        var nameDivClass = 'form-group';
        if (this.state.nameError) {
            nameError = <label className='control-label'>{this.state.nameError}</label>;
            nameDivClass += ' has-error';
        }

        var serverError = null;
        if (this.state.serverError) {
            serverError = <div className='form-group has-error'><label className='control-label'>{this.state.serverError}</label></div>;
        }

        var disabled = false;
        if (this.state.name.length <= 2) {
            disabled = true;
        }

        if (this.state.id.length <= 1) {
            disabled = true;
        }

        if (this.state.password.length <= 1) {
            disabled = true;
        }

        return (
            <form
                role='form'
                onSubmit={this.handleSubmit}
            >
                <div className={nameDivClass}>
                    <input
                        autoFocus={true}
                        type='text'
                        ref='teamname'
                        className='form-control'
                        placeholder={this.props.intl.formatMessage(holders.teamName)}
                        maxLength='128'
                        onChange={this.valueChange}
                        spellCheck='false'
                    />
                    {nameError}
                </div>
                <div className={nameDivClass}>
                    <input
                        className='form-control'
                        ref='id'
                        placeholder={formatMessage(holders.username)}
                        spellCheck='false'
                        onChange={this.valueChange}
                    />
                </div>
                <div className={nameDivClass}>
                    <input
                        type='password'
                        className='form-control'
                        ref='password'
                        placeholder={formatMessage(holders.pwd)}
                        spellCheck='false'
                        onChange={this.valueChange}
                    />
                </div>
                <div className='form-group'>
                    <a
                        className='btn btn-custom-login ldap btn-full'
                        key='ldap'
                        id='ldap-button'
                        href='#'
                        onClick={this.handleSubmit}
                        disabled={disabled}
                    >
                        <span className='icon'/>
                        <span>
                            <FormattedMessage
                                id='ldap_signup.ldap'
                                defaultMessage='Create team with LDAP Account'
                            />
                        </span>
                    </a>
                    {serverError}
                </div>
                <div className='form-group margin--extra-2x'>
                    <span><a href='/find_team'>
                        <FormattedMessage
                            id='ldap_signup.find'
                            defaultMessage='Find my teams'
                        />
                    </a></span>
                </div>
            </form>
        );
    }
}

LdapSignUpPage.propTypes = {
    intl: intlShape.isRequired
};

export default injectIntl(LdapSignUpPage);