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); }
public bool Invoke(int appId, int user, UserRole newRole) { var userFromDb = userRepository.GetById(user); if (userFromDb == null || userFromDb.IsSuperAdmin) { return(false); } if (newRole == UserRole.SuperAdmin) { userFromDb.IsSuperAdmin = true; userRepository.Edit(userFromDb); userRepository.Save(); return(true); } var userApp = userAppRepository.FindBy(m => m.User.Id == user && m.App.Id == appId).FirstOrDefault(); if (userApp == null) { return(false); } var userRole = (UserRole)userApp.Role; if (userRole.HasRole(newRole)) { return(false); } userApp.Role = (DataAccess.Enums.UserRole)newRole; userAppRepository.Edit(userApp); userAppRepository.Save(); return(true); }