Beispiel #1
0
        public void Save()
        {
            GlobalDebug.WriteLineIf(GlobalDebug.Info, "Principal", "Entering Save");

            // Make sure we're not disposed or deleted.
            CheckDisposedOrDeleted();

            // Make sure we're not a fake principal
            CheckFakePrincipal();

            // We must have a PrincipalContext to save into.  This should always be the case, unless we're unpersisted
            // and they never set a PrincipalContext.
            if (_ctx == null)
            {
                Debug.Assert(this.unpersisted == true);
                throw new InvalidOperationException(SR.PrincipalMustSetContextForSave);
            }

            // Call the appropriate operation depending on whether this is an insert or update
            StoreCtx storeCtxToUse = GetStoreCtxToUse();

            Debug.Assert(storeCtxToUse != null);    // since we know this.ctx isn't null

            if (this.unpersisted)
            {
                GlobalDebug.WriteLineIf(GlobalDebug.Info, "Principal", "Save: inserting principal of type {0} using {1}", this.GetType(), storeCtxToUse.GetType());
                Debug.Assert(storeCtxToUse == _ctx.ContextForType(this.GetType()));
                storeCtxToUse.Insert(this);
                this.unpersisted = false;  // once we persist, we're no longer in the unpersisted state
            }
            else
            {
                GlobalDebug.WriteLineIf(GlobalDebug.Info, "Principal", "Save: updating principal of type {0} using {1}", this.GetType(), storeCtxToUse.GetType());
                Debug.Assert(storeCtxToUse == _ctx.QueryCtx);
                storeCtxToUse.Update(this);
            }
        }
Beispiel #2
0
 public void Save()
 {
     this.CheckDisposedOrDeleted();
     this.CheckFakePrincipal();
     if (this.ctx != null)
     {
         StoreCtx storeCtxToUse = this.GetStoreCtxToUse();
         if (!this.unpersisted)
         {
             storeCtxToUse.Update(this);
             return;
         }
         else
         {
             storeCtxToUse.Insert(this);
             this.unpersisted = false;
             return;
         }
     }
     else
     {
         throw new InvalidOperationException(StringResources.PrincipalMustSetContextForSave);
     }
 }
Beispiel #3
0
        public void Save(PrincipalContext context)
        {
            GlobalDebug.WriteLineIf(GlobalDebug.Info, "Principal", "Entering Save(Context)");

            // Make sure we're not disposed or deleted.
            CheckDisposedOrDeleted();

            // Make sure we're not a fake principal
            CheckFakePrincipal();

            // We must have a PrincipalContext to save into.  This should always be the case, unless we're unpersisted
            // and they never set a PrincipalContext.
            if (context == null)
            {
                Debug.Assert(this.unpersisted == true);
                throw new InvalidOperationException(SR.NullArguments);
            }

            if (context.ContextType == ContextType.Machine || _ctx.ContextType == ContextType.Machine)
            {
                throw new InvalidOperationException(SR.SaveToNotSupportedAgainstMachineStore);
            }

            // If the user is trying to save to the same context we are already set to then just save the changes
            if (context == _ctx)
            {
                Save();
                return;
            }

            // If we already have a context set on this object then make sure the new
            // context is of the same type.
            if (context.ContextType != _ctx.ContextType)
            {
                Debug.Assert(this.unpersisted == true);
                throw new InvalidOperationException(SR.SaveToMustHaveSamecontextType);
            }

            StoreCtx originalStoreCtx = GetStoreCtxToUse();

            _ctx = context;

            // Call the appropriate operation depending on whether this is an insert or update
            StoreCtx newStoreCtx = GetStoreCtxToUse();

            Debug.Assert(newStoreCtx != null);      // since we know this.ctx isn't null
            Debug.Assert(originalStoreCtx != null); // since we know this.ctx isn't null

            if (this.unpersisted)
            {
                // We have an unpersisted principal so we just want to create a principal in the new store.
                GlobalDebug.WriteLineIf(GlobalDebug.Info, "Principal", "Save(context): inserting new principal of type {0} using {1}", this.GetType(), newStoreCtx.GetType());
                Debug.Assert(newStoreCtx == _ctx.ContextForType(this.GetType()));
                newStoreCtx.Insert(this);
                this.unpersisted = false;  // once we persist, we're no longer in the unpersisted state
            }
            else
            {
                // We have a principal that already exists.  We need to move it to the new store.
                GlobalDebug.WriteLineIf(GlobalDebug.Info, "Principal", "Save(context): Moving principal of type {0} using {1}", this.GetType(), newStoreCtx.GetType());

                // we are now saving to a new store so this principal is unpersisted.
                this.unpersisted = true;

                // If the user has modified the name save away the current name so
                // if the move succeeds and the update fails we will move the item back to the original
                // store with the original name.
                bool   nameModified = _nameChanged == LoadState.Changed;
                string previousName = null;

                if (nameModified)
                {
                    string newName = _name;
                    _ctx.QueryCtx.Load(this, PropertyNames.PrincipalName);
                    previousName = _name;
                    this.Name    = newName;
                }

                newStoreCtx.Move(originalStoreCtx, this);

                try
                {
                    this.unpersisted = false;  // once we persist, we're no longer in the unpersisted state

                    newStoreCtx.Update(this);
                }
                catch (System.SystemException e)
                {
                    try
                    {
                        GlobalDebug.WriteLineIf(GlobalDebug.Error, "Principal", "Save(context):,  Update Failed (attempting to move back) Exception {0} ", e.Message);

                        if (nameModified)
                        {
                            this.Name = previousName;
                        }

                        originalStoreCtx.Move(newStoreCtx, this);

                        GlobalDebug.WriteLineIf(GlobalDebug.Info, "Principal", "Move back succeeded");
                    }
                    catch (System.SystemException deleteFail)
                    {
                        // The move back failed.  Just continue we will throw the original exception below.
                        GlobalDebug.WriteLineIf(GlobalDebug.Error, "Principal", "Save(context):,  Move back Failed {0} ", deleteFail.Message);
                    }

                    if (e is System.Runtime.InteropServices.COMException)
                    {
                        throw ExceptionHelper.GetExceptionFromCOMException((System.Runtime.InteropServices.COMException)e);
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            _ctx.QueryCtx = newStoreCtx;  // so Updates go to the right StoreCtx
        }
Beispiel #4
0
 public void Save(PrincipalContext context)
 {
     this.CheckDisposedOrDeleted();
     this.CheckFakePrincipal();
     if (context.ContextType == ContextType.Machine || this.ctx.ContextType == ContextType.Machine)
     {
         throw new InvalidOperationException(StringResources.SaveToNotSupportedAgainstMachineStore);
     }
     else
     {
         if (context != null)
         {
             if (context != this.ctx)
             {
                 if (context.ContextType == this.ctx.ContextType)
                 {
                     StoreCtx storeCtxToUse = this.GetStoreCtxToUse();
                     this.ctx = context;
                     StoreCtx storeCtx = this.GetStoreCtxToUse();
                     if (!this.unpersisted)
                     {
                         this.unpersisted = true;
                         bool   flag = this.nameChanged == LoadState.Changed;
                         string str  = null;
                         if (flag)
                         {
                             string str1 = this.name;
                             this.ctx.QueryCtx.Load(this, "Principal.Name");
                             str       = this.name;
                             this.Name = str1;
                         }
                         storeCtx.Move(storeCtxToUse, this);
                         try
                         {
                             this.unpersisted = false;
                             storeCtx.Update(this);
                         }
                         catch (SystemException systemException2)
                         {
                             SystemException systemException = systemException2;
                             try
                             {
                                 if (flag)
                                 {
                                     this.Name = str;
                                 }
                                 storeCtxToUse.Move(storeCtx, this);
                             }
                             catch (SystemException systemException1)
                             {
                             }
                             if (systemException as COMException == null)
                             {
                                 throw systemException;
                             }
                             else
                             {
                                 throw ExceptionHelper.GetExceptionFromCOMException((COMException)systemException);
                             }
                         }
                     }
                     else
                     {
                         storeCtx.Insert(this);
                         this.unpersisted = false;
                     }
                     this.ctx.QueryCtx = storeCtx;
                     return;
                 }
                 else
                 {
                     throw new InvalidOperationException(StringResources.SaveToMustHaveSamecontextType);
                 }
             }
             else
             {
                 this.Save();
                 return;
             }
         }
         else
         {
             throw new InvalidOperationException(StringResources.NullArguments);
         }
     }
 }