Beispiel #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);
        }
        protected override void Setup()
        {
            base.Setup();

            CanGoBack = false;

            ROSecurityGroupList = ROSecurityGroupList.GetROSecurityGroupList(true);

            //need to find UserID from query string params
            int UserID              = 0;
            int OrganisationUserID  = 0;
            int ProtectedAreaUserID = 0;

            #region " Edit User\Add New Protected Area user\Add New OrganisationUser"

            if ((Page != null) && (Page.Request.Params["UserID"] != null))
            {
                UserID      = Convert.ToInt32(Singular.Encryption.DecryptString(System.Web.HttpUtility.UrlDecode(Page.Request.Params["UserID"]).Replace(" ", "+")));
                Editinguser = UserID != 0 ? METTLib.Security.UserList.GetUserList(UserID).First() : null;

                if (Editinguser != null)
                {
                    //let's check if the page redirection was from protected areas
                    if ((Page.Request.Params["OrganisationProtectedAreaID"] != null))
                    {
                        IsProtectedAreaUserInd = true;
                        //check if the user already exists before creating a new one to avoid duplicates
                        ProtectedAreaID = Convert.ToInt32(Singular.Encryption.DecryptString(System.Web.HttpUtility.UrlDecode(Page.Request.Params["ProtectedAreaID"]).Replace(" ", "+")));
                        var OrganisationProtectedAreaID = Convert.ToInt32(Singular.Encryption.DecryptString(System.Web.HttpUtility.UrlDecode(Page.Request.Params["OrganisationProtectedAreaID"]).Replace(" ", "+")));
                        var ProtectedAreaUser           = OrganisationProtectedAreaUserList.GetOrganisationProtectedAreaUserList().FirstOrDefault(c => c.OrganisationIDProtectedAreaID == OrganisationProtectedAreaID && c.UserID == UserID);
                        ProtectedAreaUserID = ProtectedAreaUser != null?OrganisationProtectedAreaUserList.GetOrganisationProtectedAreaUserList().FirstOrDefault(c => c.OrganisationIDProtectedAreaID == OrganisationProtectedAreaID && c.UserID == UserID).OrganisationProtectedAreaUserID : 0;

                        if (ProtectedAreaUserID != 0)
                        {
                            OrganisationProtectedAreaUser = OrganisationProtectedAreaUserList.GetOrganisationProtectedAreaUserList(ProtectedAreaUserID).FirstOrDefault();
                        }

                        if (OrganisationProtectedAreaUser != null)
                        {
                            SecurityGroupProtectedAreaUserList = SecurityGroupProtectedAreaUserList.GetSecurityGroupProtectedAreaUserList(UserID, ProtectedAreaID);
                            //mark as Active those roles that exists
                            if ((SecurityGroupProtectedAreaUserList != null) && (SecurityGroupProtectedAreaUserList.Count > 0))
                            {
                                foreach (var item in ROSecurityGroupList)
                                {
                                    if (SecurityGroupProtectedAreaUserList.Any(c => c.SecurityGroupID == item.SecurityGroupID && c.IsActiveInd == true))
                                    {
                                        item.IsActiveInd = true;
                                    }
                                }
                            }
                        }
                        else
                        {
                            OrganisationProtectedAreaUser = METTLib.ProtectedArea.OrganisationProtectedAreaUser.NewOrganisationProtectedAreaUser();
                            OrganisationProtectedAreaUser.OrganisationIDProtectedAreaID = Convert.ToInt32(Singular.Encryption.DecryptString(System.Web.HttpUtility.UrlDecode(Page.Request.Params["OrganisationProtectedAreaID"]).Replace(" ", "+")));
                            OrganisationProtectedAreaUser.UserID = UserID;

                            SecurityGroupProtectedAreaUserList = SecurityGroupProtectedAreaUserList.NewSecurityGroupProtectedAreaUserList();
                        }
                    }

                    //let's check if the page redirection was from organisations
                    else if ((Page.Request.Params["OrganisationID"] != null))
                    {
                        IsOrganisationUsersInd = true;
                        //check if the user already exists before creating a new one to avoid duplicates
                        OrganisationID   = Convert.ToInt32(Singular.Encryption.DecryptString(System.Web.HttpUtility.UrlDecode(Page.Request.Params["OrganisationID"]).Replace(" ", "+")));
                        OrganisationUser = OrganisationUserList.GetOrganisationUserList().FirstOrDefault(c => c.OrganisationID == OrganisationID && c.UserID == UserID);

                        if (OrganisationUser == null)
                        {
                            OrganisationUser = OrganisationUser.NewOrganisationUser();
                            OrganisationUser.OrganisationID = OrganisationID;
                            OrganisationUser.UserID         = UserID;

                            SecurityGroupOrganisationUserList = SecurityGroupOrganisationUserList.NewSecurityGroupOrganisationUserList();
                        }
                        else
                        {
                            SecurityGroupOrganisationUserList = SecurityGroupOrganisationUserList.GetSecurityGroupOrganisationUserList(UserID, OrganisationID);
                            //mark as Active the roles that exists
                            if ((SecurityGroupProtectedAreaUserList != null) && (SecurityGroupProtectedAreaUserList.Count > 0))
                            {
                                foreach (var item in ROSecurityGroupList)
                                {
                                    if (SecurityGroupProtectedAreaUserList.Any(c => c.SecurityGroupID == item.SecurityGroupID && c.IsActiveInd == true))
                                    {
                                        item.IsActiveInd = true;
                                    }
                                }
                            }
                        }
                    }

                    else
                    {
                        //editing User
                        SecurityGroupUserList = SecurityGroupUserList.GetSecurityGroupUserList(UserID);

                        if ((SecurityGroupUserList != null) && (SecurityGroupUserList.Count > 0))
                        {
                            foreach (var item in ROSecurityGroupList)
                            {
                                if (SecurityGroupUserList.Any(c => c.SecurityGroupID == item.SecurityGroupID))
                                {
                                    item.IsActiveInd = true;
                                }
                            }
                        }
                    }
                }
            }

            #endregion


            #region " Edit Organisation User - From Organisation Profile "
            if ((Page != null) && (Page.Request.Params["OrganisationUserID"] != null))
            {
                OrganisationUserID = Convert.ToInt32(Singular.Encryption.DecryptString(System.Web.HttpUtility.UrlDecode(Page.Request.Params["OrganisationUserID"]).Replace(" ", "+")));
                OrganisationID     = Convert.ToInt32(Singular.Encryption.DecryptString(System.Web.HttpUtility.UrlDecode(Page.Request.Params["OrganisationID"]).Replace(" ", "+")));

                OrganisationUser = OrganisationUserList.GetOrganisationUserList().FirstOrDefault(c => c.OrganisationUserID == OrganisationUserID);

                if (OrganisationUser != null)
                {
                    UserID                 = OrganisationUser.UserID;
                    Editinguser            = UserID != 0 ? METTLib.Security.UserList.GetUserList(UserID).FirstOrDefault() : null;
                    IsOrganisationUsersInd = true;

                    SecurityGroupOrganisationUserList = SecurityGroupOrganisationUserList.GetSecurityGroupOrganisationUserList(UserID, OrganisationID);

                    //mark as Active those roles that exists
                    if ((SecurityGroupOrganisationUserList != null) && (SecurityGroupOrganisationUserList.Count > 0))
                    {
                        foreach (var item in ROSecurityGroupList)
                        {
                            if (SecurityGroupOrganisationUserList.Any(c => c.SecurityGroupID == item.SecurityGroupID && c.IsActiveInd == true))
                            {
                                item.IsActiveInd = true;
                            }
                        }
                    }
                }
            }

            #endregion


            #region " Edit Organisation Protected Area User - From Protected Area Profile"

            if ((Page != null) && (Page.Request.Params["OrganisationProtectedAreaUserID"] != null))
            {
                ProtectedAreaUserID = Convert.ToInt32(Singular.Encryption.DecryptString(System.Web.HttpUtility.UrlDecode(Page.Request.Params["OrganisationProtectedAreaUserID"]).Replace(" ", "+")));
                ProtectedAreaID     = Convert.ToInt32(Singular.Encryption.DecryptString(System.Web.HttpUtility.UrlDecode(Page.Request.Params["ProtectedAreaID"]).Replace(" ", "+")));

                OrganisationProtectedAreaUser = OrganisationProtectedAreaUserList.GetOrganisationProtectedAreaUserList(ProtectedAreaUserID).FirstOrDefault();

                if (OrganisationProtectedAreaUser != null)
                {
                    UserID = OrganisationProtectedAreaUser.UserID;

                    Editinguser            = UserID != 0 ? METTLib.Security.UserList.GetUserList(UserID).FirstOrDefault() : null;
                    IsProtectedAreaUserInd = true;

                    //ROSecurityGroupProtectedAreaUserList = ROSecurityGroupProtectedAreaUserList.GetROSecurityGroupProtectedAreaUserList(UserID,ProtectedAreaID);

                    SecurityGroupProtectedAreaUserList = SecurityGroupProtectedAreaUserList.GetSecurityGroupProtectedAreaUserList(UserID, ProtectedAreaID);

                    //mark as Active those roles that exists
                    if ((SecurityGroupProtectedAreaUserList != null) && (SecurityGroupProtectedAreaUserList.Count > 0))
                    {
                        foreach (var item in ROSecurityGroupList)
                        {
                            if (SecurityGroupProtectedAreaUserList.Any(c => c.SecurityGroupID == item.SecurityGroupID && c.IsActiveInd == true))
                            {
                                item.IsActiveInd = true;
                            }
                        }
                    }
                }
            }

            #endregion

            if (Editinguser == null)
            {
                //new user
                //Editinguser = METTLib.Security.UserList.GetUserList.new
            }
        }