Пример #1
0
        public bool Invoke(int appId, int user, UserRole newRole)
        {
            var userFromDB = userRepository.GetById(user);

            if (userFromDB == null || userFromDB.IsSuperAdmin && newRole == UserRole.SuperAdmin)
            {
                return(false);
            }

            var userApp = userAppRepository.FindBy(m => m.User.Id == user && m.App.Id == appId).FirstOrDefault();

            if (userApp == null && !userFromDB.IsSuperAdmin)
            {
                return(false);
            }

            if (userApp == null && userFromDB.IsSuperAdmin)
            {
                // Set IsSuperAdmin flag to false
                userFromDB.IsSuperAdmin = false;
                userRepository.Edit(userFromDB);
                userRepository.Save();

                // Add new user to this app with newRole
                var newUserApp = new UserAppModel
                {
                    AppId  = appId,
                    UserId = user,
                    Role   = newRole
                };

                addNewUserApp.Invoke(newUserApp);

                return(true);
            }

            var userRole = (UserRole)userApp.Role;

            if (userRole.Equals(newRole) || newRole > userRole)
            {
                if (!userFromDB.IsSuperAdmin)
                {
                    return(false);
                }

                // Set IsSuperAdmin flag to false
                userFromDB.IsSuperAdmin = false;
                userRepository.Edit(userFromDB);
                userRepository.Save();

                return(true);
            }

            userApp.Role = (DataAccess.Enums.UserRole)newRole;
            userAppRepository.Edit(userApp);
            userAppRepository.Save();

            return(true);
        }
Пример #2
0
        public void SeedUserApps(IAddNewUserApp addNewUserApps)
        {
            foreach (var user in users)
            {
                var model = new UserAppModel
                {
                    AppId  = context.Apps.OrderBy(r => Guid.NewGuid()).Take(1).FirstOrDefault().Id,
                    UserId = context.Users.FirstOrDefault(r => r.Name.Contains(user)).Id
                };

                switch (user)
                {
                case "manager":
                    model.Role = UserRole.Manager;
                    addNewUserApps.Invoke(model);
                    break;

                case "user":
                    model.Role = UserRole.User;
                    addNewUserApps.Invoke(model);
                    break;
                }
            }
        }
Пример #3
0
        public IActionResult AddUser(AppUserViewModel appuser)
        {
            if (!ModelState.IsValid)
            {
                Alert.Warning();
            }

            var newAppuser = new UserAppModel
            {
                AppId  = appuser.AppId,
                UserId = appuser.UserId,
                Role   = appuser.Role
            };

            addUserApp.Invoke(newAppuser);

            return(RedirectToAction("Details", new { id = appuser.AppId }));
        }