summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Security/Permissions/IsolatedStorageFilePermission.cs
blob: 42bc648c724dbaf5fe67ec720b687158720fb146 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

//
// 
// Purpose : This permission is used to controls/administer access to 
//  IsolatedStorageFile
//

namespace System.Security.Permissions {

    using System.Globalization;
    
    [Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
    sealed public class IsolatedStorageFilePermission : IsolatedStoragePermission, IBuiltInPermission
    {
        public IsolatedStorageFilePermission(PermissionState state)
        : base(state) { }

        internal IsolatedStorageFilePermission(IsolatedStorageContainment UsageAllowed, 
            long ExpirationDays, bool PermanentData)
        : base(UsageAllowed, ExpirationDays, PermanentData) { }

        //------------------------------------------------------
        //
        // IPERMISSION IMPLEMENTATION
        //
        //------------------------------------------------------
        
        public override IPermission Union(IPermission target)
        {
            if (target == null)
            {
                return this.Copy();
            }
            else if (!VerifyType(target))
            {
                throw new 
                    ArgumentException(
                                    Environment.GetResourceString("Argument_WrongType", this.GetType().FullName)
                                     );
            }
            
            IsolatedStorageFilePermission operand = (IsolatedStorageFilePermission)target;
    
            if (this.IsUnrestricted() || operand.IsUnrestricted()) 
            {
                return new IsolatedStorageFilePermission( PermissionState.Unrestricted );
            }
            else
            {
                IsolatedStorageFilePermission union;
                union = new IsolatedStorageFilePermission( PermissionState.None );
                union.m_userQuota = max(m_userQuota,operand.m_userQuota);   
                union.m_machineQuota = max(m_machineQuota,operand.m_machineQuota);  
                union.m_expirationDays = max(m_expirationDays,operand.m_expirationDays);    
                union.m_permanentData = m_permanentData || operand.m_permanentData; 
                union.m_allowed = (IsolatedStorageContainment)max((long)m_allowed,(long)operand.m_allowed); 
                return union;
            }
        }   

        public override bool IsSubsetOf(IPermission target)
        {
            if (target == null)
            {
                return ((m_userQuota == 0) &&
                        (m_machineQuota == 0) &&
                        (m_expirationDays == 0) &&
                        (m_permanentData == false) &&
                        (m_allowed == IsolatedStorageContainment.None));
            }

            try
            {
                IsolatedStorageFilePermission operand = (IsolatedStorageFilePermission)target;

                if (operand.IsUnrestricted())
                    return true;

                return ((operand.m_userQuota >= m_userQuota) &&
                        (operand.m_machineQuota >= m_machineQuota) &&
                        (operand.m_expirationDays >= m_expirationDays) &&
                        (operand.m_permanentData || !m_permanentData) &&
                        (operand.m_allowed >= m_allowed));
            }
            catch (InvalidCastException)
            {
                throw new 
                    ArgumentException(
                                    Environment.GetResourceString("Argument_WrongType", this.GetType().FullName)
                                     );
            }                

        }
        
        public override IPermission Intersect(IPermission target)
        {
            if (target == null)
                return null;
            else if (!VerifyType(target))
            {
                throw new 
                    ArgumentException(
                                    Environment.GetResourceString("Argument_WrongType", this.GetType().FullName)
                                     );
            }

            IsolatedStorageFilePermission operand = (IsolatedStorageFilePermission)target;

            if(operand.IsUnrestricted()) 
                return Copy();
            else if(IsUnrestricted())
                return target.Copy();
            
            IsolatedStorageFilePermission intersection;
            intersection = new IsolatedStorageFilePermission( PermissionState.None );
            intersection.m_userQuota = min(m_userQuota,operand.m_userQuota);    
            intersection.m_machineQuota = min(m_machineQuota,operand.m_machineQuota);   
            intersection.m_expirationDays = min(m_expirationDays,operand.m_expirationDays); 
            intersection.m_permanentData = m_permanentData && operand.m_permanentData;  
            intersection.m_allowed = (IsolatedStorageContainment)min((long)m_allowed,(long)operand.m_allowed);  

            if ((intersection.m_userQuota == 0) &&
            (intersection.m_machineQuota == 0) &&
            (intersection.m_expirationDays == 0) &&
            (intersection.m_permanentData == false) &&
            (intersection.m_allowed == IsolatedStorageContainment.None))
                return null;

            return intersection;
        }
      
        public override IPermission Copy()
        {
            IsolatedStorageFilePermission copy ;
            copy = new IsolatedStorageFilePermission(PermissionState.Unrestricted);
            if(!IsUnrestricted()){
                copy.m_userQuota = m_userQuota; 
                copy.m_machineQuota = m_machineQuota;   
                copy.m_expirationDays = m_expirationDays;   
                copy.m_permanentData = m_permanentData; 
                copy.m_allowed = m_allowed; 
            }
            return copy;
        }


        /// <internalonly/>
        int IBuiltInPermission.GetTokenIndex()
        {
            return IsolatedStorageFilePermission.GetTokenIndex();
        }

        internal static int GetTokenIndex()
        {
            return BuiltInPermissionIndex.IsolatedStorageFilePermissionIndex;
        }
    }
}