2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / FirebirdSql.Data.Firebird / FirebirdSql.Data.Firebird / Services / FbSecurity.cs
1 /*
2  *      Firebird ADO.NET Data provider for .NET and     Mono 
3  * 
4  *         The contents of this file are subject to     the     Initial 
5  *         Developer's Public License Version 1.0 (the "License"); 
6  *         you may not use this file except     in compliance with the 
7  *         License.     You     may     obtain a copy of the License at 
8  *         http://www.firebirdsql.org/index.php?op=doc&id=idpl
9  *
10  *         Software     distributed     under the License is distributed on     
11  *         an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either 
12  *         express or implied.  See     the     License for     the     specific 
13  *         language     governing rights and limitations under the License.
14  * 
15  *      Copyright (c) 2002,     2004 Carlos     Guzman Alvarez
16  *      All     Rights Reserved.
17  */
18
19 using System;
20 using FirebirdSql.Data.Common;
21
22 namespace FirebirdSql.Data.Firebird.Services
23 {
24         ///     <include file='Doc/en_EN/FbSecurity.xml' path='doc/class[@name="FbSecurity"]/overview/*'/>
25         public sealed class     FbSecurity : FbService
26         {                               
27                 #region Properties
28
29                 ///     <include file='Doc/en_EN/FbSecurity.xml' path='doc/class[@name="FbSecurity"]/property[@name="UsersDbPath"]/*'/>
30                 public string UsersDbPath
31                 {
32                         get
33                         {
34                                 byte[] buffer = this.QueryService(
35                                         new     byte[] {IscCodes.isc_info_svc_user_dbpath});
36                                 System.Collections.ArrayList info =     this.ParseQueryInfo(buffer);
37                         
38                                 return info.Count != 0 ? (string)info[0] : null;
39                         }                       
40                 }
41                 
42                 #endregion
43
44                 #region Constructors
45
46                 ///     <include file='Doc/en_EN/FbSecurity.xml' path='doc/class[@name="FbSecurity"]/constructor[@name="FbSecurity"]/*'/>
47                 public FbSecurity()     : base()
48                 {
49                 }
50                 
51                 #endregion              
52                 
53                 #region Methods
54
55                 ///     <include file='Doc/en_EN/FbSecurity.xml' path='doc/class[@name="FbSecurity"]/method[@name="AddUser(FbUserData)"]/*'/>
56                 public void     AddUser(FbUserData user)
57                 {
58                         if (user.UserName != null && user.UserName.Length == 0)
59                         {
60                                 throw new InvalidOperationException("Invalid user name.");
61                         }
62                         if (user.UserPassword != null && user.UserPassword.Length == 0)
63                         {
64                                 throw new InvalidOperationException("Invalid user password.");
65                         }
66
67                         // Configure Spb
68                         this.StartSpb = this.CreateParameterBuffer();
69
70                         this.StartSpb.Append(IscCodes.isc_action_svc_add_user);
71                                                 
72                         this.StartSpb.Append(IscCodes.isc_spb_sec_username,     user.UserName);
73                         this.StartSpb.Append(IscCodes.isc_spb_sec_password,     user.UserPassword);
74
75                         if (user.FirstName != null && user.FirstName.Length     > 0)
76                         {
77                                 this.StartSpb.Append(IscCodes.isc_spb_sec_firstname, user.FirstName);
78                         }
79
80                         if (user.MiddleName     != null && user.MiddleName.Length >     0)
81                         {
82                                 this.StartSpb.Append(IscCodes.isc_spb_sec_middlename, user.MiddleName);
83                         }
84
85                         if (user.LastName != null && user.LastName.Length >     0)
86                         {
87                                 this.StartSpb.Append(IscCodes.isc_spb_sec_lastname,     user.LastName);
88                         }
89
90                         if (user.UserID != 0)
91                         {
92                                 this.StartSpb.Append(IscCodes.isc_spb_sec_userid, user.UserID);
93                         }
94
95                         if (user.GroupID !=     0)
96                         {
97                                 this.StartSpb.Append(IscCodes.isc_spb_sec_groupid, user.GroupID);
98                         }
99
100                         if (user.GroupName != null && user.GroupName.Length     > 0)
101                         {
102                                 this.StartSpb.Append(IscCodes.isc_spb_sec_groupname, user.GroupName);
103                         }
104
105                         if (user.RoleName != null && user.RoleName.Length >     0)
106                         {
107                                 this.StartSpb.Append(IscCodes.isc_spb_sql_role_name, user.RoleName);
108                         }
109                         
110                         // Start execution
111                         this.StartTask();
112                                                 
113                         this.Close();
114                 }
115
116                 ///     <include file='Doc/en_EN/FbSecurity.xml' path='doc/class[@name="FbSecurity"]/method[@name="DeleteUser(FbUserData)"]/*'/>
117                 public void     DeleteUser(FbUserData user)
118                 {
119                         if (user.UserName != null && user.UserName.Length == 0)
120                         {
121                                 throw new InvalidOperationException("Invalid user name.");
122                         }
123                         
124                         // Configure Spb
125                         this.StartSpb = this.CreateParameterBuffer();
126
127                         this.StartSpb.Append(IscCodes.isc_action_svc_delete_user);
128
129                         this.StartSpb.Append(IscCodes.isc_spb_sec_username,     user.UserName);
130                         
131                         if (user.RoleName != null && user.RoleName.Length >     0)
132                         {
133                                 this.StartSpb.Append(IscCodes.isc_spb_sql_role_name, user.RoleName);
134                         }
135                         
136                         // Start execution
137                         this.StartTask();
138                                                 
139                         this.Close();                   
140                 }
141
142                 ///     <include file='Doc/en_EN/FbSecurity.xml' path='doc/class[@name="FbSecurity"]/method[@name="ModifyUser(FbUserData)"]/*'/>
143                 public void     ModifyUser(FbUserData user)
144                 {
145                         if (user.UserName != null && user.UserName.Length == 0)
146                         {
147                                 throw new InvalidOperationException("Invalid user name.");
148                         }
149                         if (user.UserPassword != null && user.UserPassword.Length == 0)
150                         {
151                                 throw new InvalidOperationException("Invalid user password.");
152                         }
153
154                         // Configure Spb
155                         this.StartSpb = this.CreateParameterBuffer();
156
157                         this.StartSpb.Append(IscCodes.isc_action_svc_modify_user);
158                         this.StartSpb.Append(IscCodes.isc_spb_sec_username,     user.UserName);
159
160                         if (user.UserPassword != null && user.UserPassword.Length >     0)
161                         {
162                                 this.StartSpb.Append(IscCodes.isc_spb_sec_password,     user.UserPassword);
163                         }
164
165                         if (user.FirstName != null && user.FirstName.Length     > 0)
166                         {
167                                 this.StartSpb.Append(IscCodes.isc_spb_sec_firstname, user.FirstName);
168                         }
169
170                         if (user.MiddleName     != null && user.MiddleName.Length >     0)
171                         {
172                                 this.StartSpb.Append(IscCodes.isc_spb_sec_middlename, user.MiddleName);
173                         }
174
175                         if (user.LastName != null && user.LastName.Length >     0)
176                         {
177                                 this.StartSpb.Append(IscCodes.isc_spb_sec_lastname,     user.LastName);
178                         }
179
180                         this.StartSpb.Append(IscCodes.isc_spb_sec_userid, user.UserID);                         
181                         this.StartSpb.Append(IscCodes.isc_spb_sec_groupid, user.GroupID);
182                         
183                         if (user.GroupName != null && user.GroupName.Length     > 0)
184                         {
185                                 this.StartSpb.Append(IscCodes.isc_spb_sec_groupname, user.GroupName);
186                         }
187
188                         if (user.RoleName != null && user.RoleName.Length >     0)
189                         {
190                                 this.StartSpb.Append(IscCodes.isc_spb_sql_role_name, user.RoleName);
191                         }
192                         
193                         // Start execution
194                         this.StartTask();
195                                                 
196                         this.Close();                   
197                 }
198
199                 ///     <include file='Doc/en_EN/FbSecurity.xml' path='doc/class[@name="FbSecurity"]/method[@name="DisplayUser(System.String)"]/*'/>
200                 public FbUserData DisplayUser(string userName)
201                 {
202                         // Configure Spb
203                         this.StartSpb = this.CreateParameterBuffer();
204
205                         this.StartSpb.Append(IscCodes.isc_action_svc_display_user);                     
206                         this.StartSpb.Append(IscCodes.isc_spb_sec_username,     userName);
207                                                 
208                         // Start execution
209                         this.StartTask();
210
211                         byte[] buffer = this.QueryService(
212                                 new     byte[] {IscCodes.isc_info_svc_get_users});
213
214                         System.Collections.ArrayList info =     base.ParseQueryInfo(buffer);
215                         
216                         this.Close();
217                         
218                         if (info.Count == 0)
219                         {
220                                 return null;
221                         }
222
223                         FbUserData[] users = (FbUserData[])info[0];
224
225                         return (users != null && users.Length > 0) ? users[0] : null;
226                 }
227
228                 ///     <include file='Doc/en_EN/FbSecurity.xml' path='doc/class[@name="FbSecurity"]/method[@name="DisplayUsers"]/*'/>
229                 public FbUserData[]     DisplayUsers()
230                 {
231                         // Configure Spb
232                         this.StartSpb = this.CreateParameterBuffer();
233
234                         this.StartSpb.Append(IscCodes.isc_action_svc_display_user);                     
235                                                 
236                         // Start execution
237                         this.StartTask();
238
239                         byte[] buffer = this.QueryService(
240                                 new     byte[] {IscCodes.isc_info_svc_get_users});
241                         
242                         System.Collections.ArrayList info =     base.ParseQueryInfo(buffer);
243                         
244                         this.Close();
245                 
246                         if (info.Count == 0)
247                         {
248                                 return null;
249                         }
250
251                         return (FbUserData[])info[0];
252                 }
253                 
254                 #endregion
255         }
256 }