From 3d5bf17349fb139f627bc4916d6f704ad159d5a6 Mon Sep 17 00:00:00 2001
From: Tatsuya Niwa <tty.niwa@gmail.com>
Date: Sun, 24 Jan 2016 23:54:49 +0900
Subject: PLT-588 Add a warning when a user demotes themselves from System
 Admin to another role.

---
 web/react/components/admin_console/user_item.jsx | 111 ++++++++++++++++++-----
 1 file changed, 86 insertions(+), 25 deletions(-)

(limited to 'web/react/components/admin_console/user_item.jsx')

diff --git a/web/react/components/admin_console/user_item.jsx b/web/react/components/admin_console/user_item.jsx
index 59b4861e4..9220b2cc3 100644
--- a/web/react/components/admin_console/user_item.jsx
+++ b/web/react/components/admin_console/user_item.jsx
@@ -3,6 +3,8 @@
 
 import * as Client from '../../utils/client.jsx';
 import * as Utils from '../../utils/utils.jsx';
+import UserStore from '../../stores/user_store.jsx';
+import DemoteOwnRoleModal from './demote_own_role_modal.jsx';
 
 import {FormattedMessage} from 'mm-intl';
 
@@ -16,25 +18,38 @@ export default class UserItem extends React.Component {
         this.handleMakeAdmin = this.handleMakeAdmin.bind(this);
         this.handleMakeSystemAdmin = this.handleMakeSystemAdmin.bind(this);
         this.handleResetPassword = this.handleResetPassword.bind(this);
+        this.doDemote = this.doDemote.bind(this);
+        this.doDemoteSubmit = this.doDemoteSubmit.bind(this);
+        this.doDemoteDismiss = this.doDemoteDismiss.bind(this);
 
-        this.state = {};
+        this.state = {
+            serverError: null,
+            showDemoteModal: false,
+            user: null,
+            role: null
+        };
     }
 
     handleMakeMember(e) {
         e.preventDefault();
-        const data = {
-            user_id: this.props.user.id,
-            new_roles: ''
-        };
+        var me = UserStore.getCurrentUser();
+        if (this.props.user.id === me.id) {
+            this.doDemote(this.props.user, '');
+        } else {
+            const data = {
+                user_id: this.props.user.id,
+                new_roles: ''
+            };
 
-        Client.updateRoles(data,
-            () => {
-                this.props.refreshProfiles();
-            },
-            (err) => {
-                this.setState({serverError: err.message});
-            }
-        );
+            Client.updateRoles(data,
+                () => {
+                    this.props.refreshProfiles();
+                },
+                (err) => {
+                    this.setState({serverError: err.message});
+                }
+            );
+        }
     }
 
     handleMakeActive(e) {
@@ -63,19 +78,24 @@ export default class UserItem extends React.Component {
 
     handleMakeAdmin(e) {
         e.preventDefault();
-        const data = {
-            user_id: this.props.user.id,
-            new_roles: 'admin'
-        };
+        var me = UserStore.getCurrentUser();
+        if (this.props.user.id === me.id) {
+            this.doDemote(this.props.user, 'admin');
+        } else {
+            const data = {
+                user_id: this.props.user.id,
+                new_roles: 'admin'
+            };
 
-        Client.updateRoles(data,
-            () => {
-                this.props.refreshProfiles();
-            },
-            (err) => {
-                this.setState({serverError: err.message});
-            }
-        );
+            Client.updateRoles(data,
+                () => {
+                    this.props.refreshProfiles();
+                },
+                (err) => {
+                    this.setState({serverError: err.message});
+                }
+            );
+        }
     }
 
     handleMakeSystemAdmin(e) {
@@ -100,6 +120,33 @@ export default class UserItem extends React.Component {
         this.props.doPasswordReset(this.props.user);
     }
 
+    doDemote(user, role) {
+        this.setState({
+            serverError: this.state.serverError,
+            showDemoteModal: true,
+            user,
+            role
+        });
+    }
+
+    doDemoteDismiss() {
+        this.setState({
+            serverError: this.state.serverError,
+            showDemoteModal: false,
+            user: null,
+            role: null
+        });
+    }
+
+    doDemoteSubmit() {
+        this.setState({
+            serverError: this.state.serverError,
+            showDemoteModal: false,
+            user: null,
+            role: null
+        });
+    }
+
     render() {
         let serverError = null;
         if (this.state.serverError) {
@@ -247,6 +294,19 @@ export default class UserItem extends React.Component {
                 </li>
             );
         }
+        var me = UserStore.getCurrentUser();
+        let makeDemoteModal = null;
+        if (this.props.user.id === me.id) {
+            makeDemoteModal = (
+                <DemoteOwnRoleModal
+                    user={this.state.user}
+                    role={this.state.role}
+                    show={this.state.showDemoteModal}
+                    onModalSubmit={this.doDemoteSubmit}
+                    onModalDismissed={this.doDemoteDismiss}
+                />
+            );
+        }
 
         return (
             <tr>
@@ -293,6 +353,7 @@ export default class UserItem extends React.Component {
                             </li>
                         </ul>
                     </div>
+                    {makeDemoteModal}
                     {serverError}
                 </td>
             </tr>
-- 
cgit v1.2.3-1-g7c22

