AppRoleController.js 3.73 KB
Newer Older
lepdou committed
1 2 3 4 5 6 7 8 9
role_module.controller('AppRoleController',
                       ['$scope', '$location', '$window', 'toastr', 'AppService', 'AppUtil', 'PermissionService',
                        function ($scope, $location, $window, toastr, AppService, AppUtil, PermissionService) {

                            var params = AppUtil.parseParams($location.$$url);
                            $scope.pageContext = {
                                appId: params.appid
                            };

10 11
                            $scope.userSelectWidgetId = 'toAssignMasterRoleUser';

lepdou committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
                            PermissionService.has_assign_user_permission($scope.pageContext.appId)
                                .then(function (result) {
                                    $scope.hasAssignUserPermission = result.hasPermission;
                                }, function (reslt) {

                                });

                            PermissionService.get_app_role_users($scope.pageContext.appId)
                                .then(function (result) {
                                    $scope.appRoleUsers = result;
                                }, function (result) {

                                });


                            $scope.assignMasterRoleToUser = function () {
lepdou committed
28 29 30 31 32 33
                                var user = $('.' + $scope.userSelectWidgetId).select2('data')[0];
                                if (!user){
                                    toastr.warning("请选择用户");
                                    return;
                                }
                                var toAssignMasterRoleUser = user.id;
lepdou committed
34
                                PermissionService.assign_master_role($scope.pageContext.appId,
35
                                                                     toAssignMasterRoleUser)
lepdou committed
36
                                    .then(function (result) {
lepdou committed
37
                                        toastr.success("添加成功");
38
                                        $scope.appRoleUsers.masterUsers.push({userId: toAssignMasterRoleUser});
39
                                        $('.' + $scope.userSelectWidgetId).select2("val", "");
lepdou committed
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
                                    }, function (result) {
                                        toastr.error(AppUtil.errorMsg(result), "添加失败");
                                    });
                            };

                            $scope.removeMasterRoleFromUser = function (user) {
                                if ($scope.appRoleUsers.masterUsers.length <= 1) {
                                    $('#warning').modal('show');
                                    return;
                                }
                                PermissionService.remove_master_role($scope.pageContext.appId, user)
                                    .then(function (result) {
                                        toastr.success("删除成功");
                                        removeUserFromList($scope.appRoleUsers.masterUsers, user);
                                    }, function (result) {
                                        toastr.error(AppUtil.errorMsg(result), "删除失败");
                                    });
                            };

                            function removeUserFromList(list, user) {
                                var index = 0;
                                for (var i = 0; i < list.length; i++) {
                                    if (list[i].userId == user) {
                                        index = i;
                                        break;
                                    }
                                }
                                list.splice(index, 1);
                            }

                        }]);