2005-08-02 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / corlib / System.Security.Principal / WindowsIdentity.cs
index 99ac3a2623376fee46213ca6d7f47b4a4d2db0d6..4d87ce642e272f4cc19e3098917c08c683cbb86f 100644 (file)
@@ -7,11 +7,7 @@
 //
 // (C) 2002 Ximian, Inc (http://www.ximian.com)
 // Portions (C) 2003 Motus Technologies Inc. (http://www.motus.com)
-// (C) 2004 Novell (http://www.novell.com)
-//
-
-//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using System;
 using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
 using System.Runtime.Serialization;
+using System.Security.Permissions;
 
 namespace System.Security.Principal {
 
        [Serializable]
 #if NET_1_0
        public class WindowsIdentity : IIdentity, IDeserializationCallback {
+#elif NET_2_0
+       [ComVisible (true)]
+       public class WindowsIdentity : IIdentity, IDeserializationCallback, ISerializable, IDisposable {
 #else
        public class WindowsIdentity : IIdentity, IDeserializationCallback, ISerializable {
 #endif
@@ -56,21 +56,25 @@ namespace System.Security.Principal {
                // that seems to be the value used for (at least) AIX and MacOSX
                static private IntPtr invalidPosix = (IntPtr) unchecked (-2);
 
+               [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
                public WindowsIdentity (IntPtr userToken) 
                        : this (userToken, null, WindowsAccountType.Normal, false)
                {
                }
 
+               [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
                public WindowsIdentity (IntPtr userToken, string type) 
                        : this (userToken, type, WindowsAccountType.Normal, false)
                {
                }
 
+               [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
                public WindowsIdentity (IntPtr userToken, string type, WindowsAccountType acctType)
                        : this (userToken, type, acctType, false)
                {
                }
 
+               [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
                public WindowsIdentity (IntPtr userToken, string type, WindowsAccountType acctType, bool isAuthenticated)
                {
                        _type = type;
@@ -81,11 +85,13 @@ namespace System.Security.Principal {
                        SetToken (userToken);
                }
 #if !NET_1_0
+               [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
                public WindowsIdentity (string sUserPrincipalName) 
                        : this (sUserPrincipalName, null)
                {
                }
 
+               [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
                public WindowsIdentity (string sUserPrincipalName, string type)
                {
                        if (sUserPrincipalName == null)
@@ -104,19 +110,33 @@ namespace System.Security.Principal {
                        SetToken (token);
                }
 
+               [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
                public WindowsIdentity (SerializationInfo info, StreamingContext context)
                {
                        _info = info;
                }
 #endif
 
+#if NET_2_0
+               [ComVisible (false)]
+               public void Dispose ()
+               {
+                       _token = IntPtr.Zero;
+               }
+               
+               [ComVisible (false)]
+               protected virtual void Dispose (bool disposing)
+               {
+                       _token = IntPtr.Zero;
+               }
+#else
                ~WindowsIdentity ()
                {
                        // clear our copy but don't close it
                        // http://www.develop.com/kbrown/book/html/whatis_windowsprincipal.html
                        _token = IntPtr.Zero;
                }
-
+#endif
                // static methods
 
                public static WindowsIdentity GetAnonymous ()
@@ -141,7 +161,19 @@ namespace System.Security.Principal {
                {
                        return new WindowsIdentity (GetCurrentToken (), null, WindowsAccountType.Normal, true);
                }
+#if NET_2_0
+               [MonoTODO ("need icall changes")]
+               public static WindowsIdentity GetCurrent (bool ifImpersonating)
+               {
+                       throw new NotImplementedException ();
+               }
 
+               [MonoTODO ("need icall changes")]
+               public static WindowsIdentity GetCurrent (TokenAccessLevels desiredAccess)
+               {
+                       throw new NotImplementedException ();
+               }
+#endif
                // methods
 
                public virtual WindowsImpersonationContext Impersonate ()
@@ -149,6 +181,7 @@ namespace System.Security.Principal {
                        return new WindowsImpersonationContext (_token);
                }
 
+               [SecurityPermission (SecurityAction.Demand, ControlPrincipal=true)]
                public static WindowsImpersonationContext Impersonate (IntPtr userToken)
                {
                        return new WindowsImpersonationContext (userToken);
@@ -196,7 +229,30 @@ namespace System.Security.Principal {
                {
                        get { return _token; }
                }
+#if NET_2_0
+               [MonoTODO ("not implemented")]
+               public IdentityReferenceCollection Groups {
+                       get { throw new NotImplementedException (); }
+               }
 
+               [MonoTODO ("not implemented")]
+               [ComVisible (false)]
+               public TokenImpersonationLevel ImpersonationLevel {
+                       get { throw new NotImplementedException (); }
+               }
+
+               [MonoTODO ("not implemented")]
+               [ComVisible (false)]
+               public SecurityIdentifier Owner {
+                       get { throw new NotImplementedException (); }
+               }
+
+               [MonoTODO ("not implemented")]
+               [ComVisible (false)]
+               public SecurityIdentifier User {
+                       get { throw new NotImplementedException (); }
+               }
+#endif
                void IDeserializationCallback.OnDeserialization (object sender)
                {
                        _token = (IntPtr) _info.GetValue ("m_userToken", typeof (IntPtr));