Exemplo n.º 1
0
        public static Result SaveUser(MELib.Security.User user)
        {
            if (user.SecurityGroupUserList.Count == 0)
            {
                //add a default security group of General User
                SecurityGroupUser securityGroupUser = SecurityGroupUser.NewSecurityGroupUser();
                securityGroupUser.SecurityGroupID = ROSecurityGroupList.GetROSecurityGroupList(true).FirstOrDefault(c => c.SecurityGroup == "General User")?.SecurityGroupID;
                user.SecurityGroupUserList.Add(securityGroupUser);
            }

            user.LoginName = user.EmailAddress;

            Result results     = new Singular.Web.Result();
            Result Saveresults = user.SaveUser(user);

            MELib.Security.User SavedUser = (MELib.Security.User)Saveresults.Data;

            if (SavedUser != null)
            {
                results.Success = true;
                results.Data    = SavedUser;
            }
            else
            {
                results.Success   = false;
                results.ErrorText = Saveresults.ErrorText;
            }
            return(results);
        }
Exemplo n.º 2
0
        public void LeaveGroup(string index)
        {
            try
            {
                //Step 1 Code to delete the object from the database
                SecurityGroupUser user = new SecurityGroupUser();
                user.userId = CurrentUser.Text;
                user.sgId   = index;
                PostRequest <SecurityGroupUser> req = new PostRequest <SecurityGroupUser>();
                req.entity = user;
                PostResponse <SecurityGroupUser> r = _accessControlService.ChildDelete <SecurityGroupUser>(req);
                if (!r.Success)
                {
                    X.MessageBox.ButtonText.Ok = Resources.Common.Ok;
                    Common.errorMessage(r);
                    return;
                }
                else
                {
                    //Step 2 :  remove the object from the store
                    UserGroupsStore.Reload();
                    AllGroupsStore.Reload();

                    //Step 3 : Showing a notification for the user
                    Notification.Show(new NotificationConfig
                    {
                        Title = Resources.Common.Notification,
                        Icon  = Icon.Information,
                        Html  = Resources.Common.RecordDeletedSucc
                    });
                }
            }
            catch (Exception ex)
            {
                //In case of error, showing a message box to the user
                X.MessageBox.ButtonText.Ok = Resources.Common.Ok;
                X.Msg.Alert(Resources.Common.Error, Resources.Common.ErrorDeletingRecord).Show();
            }
        }
Exemplo n.º 3
0
        protected void addUserToGroup(object sender, DirectEventArgs e)
        {
            PostRequest <SecurityGroupUser> user = new PostRequest <SecurityGroupUser>();
            SecurityGroupUser en = new SecurityGroupUser();

            en.userId = CurrentUser.Text;
            if (GroupsCombo.SelectedItem == null)
            {
                X.Msg.Alert(Resources.Common.Error, Resources.Common.ErrorSavingRecord).Show();
                return;
            }
            en.sgId     = GroupsCombo.SelectedItem.Value;
            user.entity = en;
            PostResponse <SecurityGroupUser> resp = _accessControlService.ChildAddOrUpdate <SecurityGroupUser>(user);

            if (!resp.Success)
            {
                Common.errorMessage(resp);
                return;
            }

            AllGroupsStore.Reload();
        }
Exemplo n.º 4
0
        public async Task <OperationResult> Update(UserRegisterModel userRegisterModel)
        {
            try
            {
                var core = await _coreHelper.GetCore();

                var sdkDbContext = core.DbContextHelper.GetDbContext();
                if (userRegisterModel.Id == 1 && _userService.UserId != 1)
                {
                    return(new OperationResult(false, _localizationService.GetString("CantEditPrimaryAdminUser")));
                }

                if (userRegisterModel.Role != EformRole.Admin && userRegisterModel.Role != EformRole.User)
                {
                    return(new OperationResult(false,
                                               _localizationService.GetString("RoleNotFound")));
                }

                var user = await _userService.GetByIdAsync(userRegisterModel.Id);

                if (user == null)
                {
                    return(new OperationResult(false,
                                               _localizationService.GetStringWithFormat("UserNotFoundUserName", userRegisterModel.UserName)));
                }

                // get role
                var roles = await _userManager.GetRolesAsync(user);

                if (user.Id == 1 && roles.Any(x => x != userRegisterModel.Role))
                {
                    return(new OperationResult(false, _localizationService.GetString("CantUpdateRoleForPrimaryAdminUser")));
                }

                var isAdmin = await _userManager.IsInRoleAsync(user, EformRole.Admin);

                if (!_dbContext.SecurityGroups.Any(x => x.Id == userRegisterModel.GroupId) && !isAdmin && userRegisterModel.Role != EformRole.Admin)
                {
                    return(new OperationResult(false,
                                               _localizationService.GetString("SecurityGroupNotFound")));
                }

                if (isAdmin && _userService.Role != EformRole.Admin)
                {
                    return(new OperationResult(false, _localizationService.GetString("YouCantViewChangeOrDeleteAdmin")));
                }

                var site = await sdkDbContext.Sites.SingleOrDefaultAsync(x => x.Name == user.FirstName + " " + user.LastName &&
                                                                         x.WorkflowState != Constants.WorkflowStates.Removed);

                var language = await sdkDbContext.Languages.SingleAsync(x => x.Id == site.LanguageId);

                await core.SiteUpdate((int)site.MicrotingUid, $"{userRegisterModel.FirstName} {userRegisterModel.LastName}", userRegisterModel.FirstName, userRegisterModel.LastName, userRegisterModel.Email, language.LanguageCode);

                user.Email          = userRegisterModel.Email;
                user.EmailConfirmed = true;
                user.UserName       = userRegisterModel.Email;
                user.FirstName      = userRegisterModel.FirstName;
                user.LastName       = userRegisterModel.LastName;

                var result = await _userManager.UpdateAsync(user);

                if (!result.Succeeded)
                {
                    return(new OperationResult(false, string.Join(" ", result.Errors.Select(x => x.Description).ToArray())));
                }

                // password
                if (userRegisterModel.Password != null)
                {
                    await _userManager.RemovePasswordAsync(user);

                    await _userManager.AddPasswordAsync(user, userRegisterModel.Password);
                }

                // change role
                if (!await _userManager.IsInRoleAsync(user, userRegisterModel.Role))
                {
                    var currentUserRole = await _userManager.GetRolesAsync(user);

                    await _userManager.RemoveFromRolesAsync(user, currentUserRole);

                    await _userManager.AddToRoleAsync(user, userRegisterModel.Role);
                }

                // Change group
                if (userRegisterModel.GroupId > 0 && user.Id > 0)
                {
                    var securityGroupUsers = _dbContext.SecurityGroupUsers
                                             .Where(x => x.EformUserId == user.Id &&
                                                    x.SecurityGroupId != userRegisterModel.GroupId);

                    _dbContext.SecurityGroupUsers.RemoveRange(securityGroupUsers);
                    if (!_dbContext.SecurityGroupUsers.Any(x =>
                                                           x.EformUserId == user.Id && x.SecurityGroupId == userRegisterModel.GroupId))
                    {
                        var securityGroupUser = new SecurityGroupUser()
                        {
                            SecurityGroupId = (int)userRegisterModel.GroupId,
                            EformUserId     = user.Id
                        };
                        _dbContext.SecurityGroupUsers.Add(securityGroupUser);
                    }

                    await _dbContext.SaveChangesAsync();
                }

                if (userRegisterModel.Role == EformRole.Admin)
                {
                    var securityGroupUsers = await _dbContext.SecurityGroupUsers.Where(x => x.EformUserId == user.Id)
                                             .ToListAsync();

                    if (securityGroupUsers.Any())
                    {
                        _dbContext.SecurityGroupUsers.RemoveRange(securityGroupUsers);

                        await _dbContext.SaveChangesAsync();
                    }
                }

                return(new OperationResult(true,
                                           _localizationService.GetStringWithFormat("UserUserNameWasUpdated", user.UserName)));
            }
            catch (Exception exception)
            {
                _logger.LogError(exception.Message);
                return(new OperationResult(false, _localizationService.GetString("ErrorWhileUpdatingUser")));
            }
        }
Exemplo n.º 5
0
        public async Task <OperationResult> Create(UserRegisterModel userRegisterModel)
        {
            try
            {
                var core = await _coreHelper.GetCore();

                var sdkDbContext = core.DbContextHelper.GetDbContext();
                if (userRegisterModel.Role != EformRole.Admin && userRegisterModel.Role != EformRole.User)
                {
                    return(new OperationResult(false,
                                               _localizationService.GetString("RoleNotFound")));
                }

                var userResult = await _userManager.FindByNameAsync(userRegisterModel.Email);

                if (userResult != null)
                {
                    return(new OperationResult(false,
                                               _localizationService.GetStringWithFormat("UserUserNameAlreadyExist", userRegisterModel.Email)));
                }

                if (userRegisterModel.Role != EformRole.Admin && !_dbContext.SecurityGroups.Any(x => x.Id == userRegisterModel.GroupId))
                {
                    return(new OperationResult(false,
                                               _localizationService.GetString("SecurityGroupNotFound")));
                }

                var user = new EformUser
                {
                    Email                        = userRegisterModel.Email,
                    UserName                     = userRegisterModel.Email,
                    FirstName                    = userRegisterModel.FirstName,
                    LastName                     = userRegisterModel.LastName,
                    Locale                       = "da",
                    EmailConfirmed               = true,
                    TwoFactorEnabled             = false,
                    IsGoogleAuthenticatorEnabled = false
                };

                var result = await _userManager.CreateAsync(user, userRegisterModel.Password);

                if (!result.Succeeded)
                {
                    return(new OperationResult(false, string.Join(" ", result.Errors.Select(x => x.Description).ToArray())));
                }

                // change role
                await _userManager.AddToRoleAsync(user, userRegisterModel.Role);

                // add to group
                if (userRegisterModel.GroupId > 0 && user.Id > 0 && userRegisterModel.Role != EformRole.Admin)
                {
                    var securityGroupUser = new SecurityGroupUser()
                    {
                        SecurityGroupId = (int)userRegisterModel.GroupId,
                        EformUserId     = user.Id
                    };
                    _dbContext.SecurityGroupUsers.Add(securityGroupUser);
                    await _dbContext.SaveChangesAsync();
                }

                var site = await sdkDbContext.Sites.SingleOrDefaultAsync(x => x.Name == userRegisterModel.FirstName + " " + userRegisterModel.LastName &&
                                                                         x.WorkflowState != Constants.WorkflowStates.Removed);

                if (site == null)
                {
                    await core.SiteCreate($"{userRegisterModel.FirstName} {userRegisterModel.LastName}", userRegisterModel.FirstName, userRegisterModel.LastName,
                                          null, "da");
                }
                if (site != null)
                {
                    site.IsLocked = true;
                    await site.Update(sdkDbContext);

                    var units = await sdkDbContext.Units.Where(x => x.SiteId == site.Id).ToListAsync();

                    foreach (Unit unit in units)
                    {
                        unit.IsLocked = true;
                        await unit.Update(sdkDbContext);
                    }
                    var worker = await sdkDbContext.Workers.SingleOrDefaultAsync(x => x.FirstName == userRegisterModel.FirstName &&
                                                                                 x.LastName == userRegisterModel.LastName &&
                                                                                 x.WorkflowState != Constants.WorkflowStates.Removed);

                    if (worker != null)
                    {
                        worker.IsLocked = true;
                        await worker.Update(sdkDbContext);
                    }
                }

                return(new OperationResult(true,
                                           _localizationService.GetStringWithFormat("UserUserNameWasCreated", user.UserName)));
            }
            catch (Exception exception)
            {
                _logger.LogError(exception.Message);
                return(new OperationResult(false, _localizationService.GetString("ErrorWhileCreatingUser")));
            }
        }
Exemplo n.º 6
0
        public async Task <OperationResult> Create(UserRegisterModel userRegisterModel)
        {
            try
            {
                if (userRegisterModel.Role != EformRole.Admin && userRegisterModel.Role != EformRole.User)
                {
                    return(new OperationResult(false,
                                               _localizationService.GetString("RoleNotFound")));
                }

                var userResult = await _userManager.FindByNameAsync(userRegisterModel.Email);

                if (userResult != null)
                {
                    return(new OperationResult(false,
                                               _localizationService.GetStringWithFormat("UserUserNameAlreadyExist", userRegisterModel.Email)));
                }

                if (userRegisterModel.Role != EformRole.Admin && !_dbContext.SecurityGroups.Any(x => x.Id == userRegisterModel.GroupId))
                {
                    return(new OperationResult(false,
                                               _localizationService.GetString("SecurityGroupNotFound")));
                }

                var user = new EformUser
                {
                    Email                        = userRegisterModel.Email,
                    UserName                     = userRegisterModel.Email,
                    FirstName                    = userRegisterModel.FirstName,
                    LastName                     = userRegisterModel.LastName,
                    EmailConfirmed               = true,
                    TwoFactorEnabled             = false,
                    IsGoogleAuthenticatorEnabled = false
                };

                var result = await _userManager.CreateAsync(user, userRegisterModel.Password);

                if (!result.Succeeded)
                {
                    return(new OperationResult(false, string.Join(" ", result.Errors.Select(x => x.Description).ToArray())));
                }

                // change role
                await _userManager.AddToRoleAsync(user, userRegisterModel.Role);

                // add to group
                if (userRegisterModel.GroupId > 0 && user.Id > 0 && userRegisterModel.Role != EformRole.Admin)
                {
                    var securityGroupUser = new SecurityGroupUser()
                    {
                        SecurityGroupId = (int)userRegisterModel.GroupId,
                        EformUserId     = user.Id
                    };
                    _dbContext.SecurityGroupUsers.Add(securityGroupUser);
                    await _dbContext.SaveChangesAsync();
                }

                return(new OperationResult(true,
                                           _localizationService.GetStringWithFormat("UserUserNameWasCreated", user.UserName)));
            }
            catch (Exception exception)
            {
                _logger.LogError(exception.Message);
                return(new OperationResult(false, _localizationService.GetString("ErrorWhileCreatingUser")));
            }
        }