public void SaveCurrentUser(BitplateUser obj)
 {
     obj.Save();
     if (obj.IsMultiSiteUser)
     {
         //userGroups & permissions hoeven niet opgeslagen bij multisite users
         obj.UserGroups.Clear();
         obj.Permissions = null;
         string domainName = getDomainName();
         UserServiceReference.UserServiceClient client = BitMetaServerServicesHelper.GetUserServiceClient();
         client.SetUserData(obj, domainName);
         //weer laden om permissions & usergroups weer op te halen, usergroups eerst null maken
         obj.UserGroups = null;
         obj.Load();
     }
     SessionObject.CurrentBitplateUser = obj;
 }
        public void SaveUser(BitplateUser obj)
        {
            //Quick password fix, kan vast mooier.
            if (!obj.IsNew)
            {
                BitplateUser currentUserObject = BaseObject.GetById <BitplateUser>(obj.ID);
                obj.Password = currentUserObject.Password;
            }

            obj.Save();
            if (obj.IsMultiSiteUser)
            {
                //userGroups & permissions hoeven niet opgeslagen bij multisite users
                obj.UserGroups.Clear();
                obj.Permissions = null;
                string domainName = getDomainName();
                UserServiceReference.UserServiceClient client = BitMetaServerServicesHelper.GetUserServiceClient();
                client.SetUserData(obj, domainName);
            }
        }
        public bool Login(string email, string password, string theme, string language, bool saveData, string md5Hash)
        {
            BitplateUserGroup userGroupAdmins;
            BitplateUserGroup userGroupEveryOne;

            CreateSystemUserGroupsIfNotExists(out userGroupAdmins, out userGroupEveryOne);

            SessionObject.CurrentSiteUser = null;
            bool returnValue = false;
            Guid SiteId;

            Guid.TryParse(ConfigurationManager.AppSettings["SiteID"], out SiteId);
            if (md5Hash == null)
            {
                md5Hash = CalculateMD5Hash(password);
            }
            BitplateUser user = BaseObject.GetFirst <BitplateUser>("Email ='" + email + "' AND Password = '******' AND Active = 1");

            if (user != null)
            {
                returnValue = true;
                SessionObject.CurrentBitplateUser = user;
                user.Theme = "bitplate";

                if (saveData)
                {
                    saveUserDataCookie(email, md5Hash); //OPSLAAN GEBRUIKERSGEGEVENS WERKT NOG NIET GOED.
                }
                else
                {
                    RemoveUserDataCookie();
                }
                if (user.IsMultiSiteUser)
                {
                    //gegevens syncen
                    //haal data op van meta server
                    string domainName = getDomainName();
                    UserServiceReference.UserServiceClient client = BitMetaServerServicesHelper.GetUserServiceClient();
                    MultiSiteUser multiSiteUser = null;
                    if (client != null)
                    {
                        multiSiteUser = client.Login(domainName, email, md5Hash);
                    }
                    //opslaan
                    if (multiSiteUser != null)
                    {
                        BitplateUser localUser = multiSiteUser.ToBitPlateUser(user.Email);
                        if (localUser.ModifiedDate > user.ModifiedDate)
                        {
                            localUser.UserGroups  = user.UserGroups;
                            localUser.Permissions = user.Permissions;
                            localUser.Save();
                            user = localUser;
                        }

                        if (multiSiteUser.IsAdmin)
                        {
                            if (!user.IsUserMemberOf(userGroupAdmins.Name))
                            {
                                user.UserGroups.Add(userGroupAdmins);
                                user.Save();
                            }
                        }
                        else
                        {
                            if (user.IsUserMemberOf(userGroupAdmins.Name))
                            {
                                BitplateUserGroup userGroup = BaseObject.GetFirst <BitplateUserGroup>("FK_Site = '" + SessionObject.CurrentSite.ID + "' AND Type=9");
                                user.UserGroups.Remove(userGroupAdmins);
                                user.Save();
                            }
                        }
                        //Gebruiker toevoegen aan systeem usergroup everyone.
                        if (!user.IsUserMemberOf(userGroupEveryOne.Name))
                        {
                            user.UserGroups.Add(userGroupEveryOne);
                            user.Save();
                        }
                        returnValue = true;
                        SessionObject.CurrentBitplateUser = user;
                    }
                    else if (multiSiteUser == null && client != null)
                    {
                        //Delete user als dit een multisiteuser is welke niet meer voorkomt in de licentie server.
                        user.Delete();
                        returnValue = false;
                    }
                    else
                    {
                        returnValue = true;
                        SessionObject.CurrentBitplateUser = user;
                    }
                }
            }
            else
            {
                //probeer in te loggen bij metaserver
                string domainName = getDomainName();
                UserServiceReference.UserServiceClient client = BitMetaServerServicesHelper.GetUserServiceClient();
                MultiSiteUser multiSiteUser = client.Login(domainName, email, md5Hash);

                if (multiSiteUser != null)
                {
                    //nieuwe user aanmaken en opslaan
                    BitplateUser localUser = multiSiteUser.ToBitPlateUser(email);
                    //usergroup toevoegen
                    if (multiSiteUser.IsAdmin)
                    {
                        localUser.UserGroups.Add(userGroupAdmins);
                    }
                    //Gebruiker toevoegen aan systeem usergroup everyone.
                    localUser.UserGroups.Add(userGroupEveryOne);
                    localUser.Save();
                    SessionObject.CurrentBitplateUser = localUser;

                    returnValue = true;
                }
            }

            return(returnValue);
        }