~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/webservices/.svn/text-base/iFolderUser.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
|
3
 
| Copyright (c) 2007 Novell, Inc.
4
 
| All Rights Reserved.
5
 
|
6
 
| This program is free software; you can redistribute it and/or
7
 
| modify it under the terms of version 2 of the GNU General Public License as
8
 
| published by the Free Software Foundation.
9
 
|
10
 
| This program is distributed in the hope that it will be useful,
11
 
| but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
| GNU General Public License for more details.
14
 
|
15
 
| You should have received a copy of the GNU General Public License
16
 
| along with this program; if not, contact Novell, Inc.
17
 
|
18
 
| To contact Novell about this file by physical or electronic mail,
19
 
| you may find current contact information at www.novell.com 
20
 
|
21
 
| Author: Rob 
22
 
|***************************************************************************/
23
 
 
24
 
using System;
25
 
using System.Collections;
26
 
using System.Security.Cryptography;
27
 
 
28
 
using Simias;
29
 
using Simias.Client;
30
 
using Simias.Storage;
31
 
using Simias.Web;
32
 
using Simias.Authentication;
33
 
 
34
 
namespace iFolder.WebService
35
 
{
36
 
        /// <summary>
37
 
        /// An iFolder User Result Set
38
 
        /// </summary>
39
 
        [Serializable]
40
 
        public class iFolderUserSet
41
 
        {
42
 
                /// <summary>
43
 
                /// An Array of iFolder Users
44
 
                /// </summary>
45
 
                public iFolderUser[] Items;
46
 
 
47
 
                /// <summary>
48
 
                /// The Total Number of iFolder Users
49
 
                /// </summary>
50
 
                public int Total;
51
 
 
52
 
                /// <summary>
53
 
                /// Default Constructor
54
 
                /// </summary>
55
 
                public iFolderUserSet()
56
 
                {
57
 
                }
58
 
 
59
 
                /// <summary>
60
 
                /// Constructor
61
 
                /// </summary>
62
 
                /// <param name="items"></param>
63
 
                /// <param name="total"></param>
64
 
                public iFolderUserSet(iFolderUser[] items, int total)
65
 
                {
66
 
                        this.Items = items;
67
 
                        this.Total = total;
68
 
                }
69
 
        }
70
 
 
71
 
        /// <summary>
72
 
        /// An iFolder User
73
 
        /// </summary>
74
 
        [Serializable]
75
 
        public class iFolderUser
76
 
        {
77
 
                /// <summary>
78
 
                /// Email Property Name
79
 
                /// </summary>
80
 
                private static string EmailProperty = "Email";
81
 
 
82
 
                private static readonly ISimiasLog log = SimiasLogManager.GetLogger(typeof(Member));
83
 
 
84
 
                /// <summary>
85
 
                /// The User ID
86
 
                /// </summary>
87
 
                public string ID;
88
 
 
89
 
                /// <summary>
90
 
                /// The User Name
91
 
                /// </summary>
92
 
                public string UserName;
93
 
 
94
 
                /// <summary>
95
 
                /// The User Preferred Full Name
96
 
                /// </summary>
97
 
                public string FullName;
98
 
 
99
 
                /// <summary>
100
 
                /// The User First Name
101
 
                /// </summary>
102
 
                public string FirstName;
103
 
 
104
 
                /// <summary>
105
 
                /// The User Last Name
106
 
                /// </summary>
107
 
                public string LastName;
108
 
                /// <summary>
109
 
                /// The User Rights in the iFolder/Domain
110
 
                /// </summary>
111
 
                public Rights MemberRights;
112
 
 
113
 
                /// <summary>
114
 
                /// Is the User's Login Enabled
115
 
                /// </summary>
116
 
                public bool Enabled;
117
 
 
118
 
                /// <summary>
119
 
                /// Is the User the Owner in the iFolder/Domain
120
 
                /// </summary>
121
 
                public bool IsOwner;
122
 
 
123
 
                /// <summary>
124
 
                /// The User Email Address
125
 
                /// </summary>
126
 
                public string Email;
127
 
 
128
 
                /// <summary>
129
 
                /// The User HomeServer Name
130
 
                /// </summary>
131
 
                public string HomeServer;
132
 
 
133
 
                /// <summary>
134
 
                /// Constructor
135
 
                /// </summary>
136
 
                public iFolderUser()
137
 
                {
138
 
                }
139
 
 
140
 
                /// <summary>
141
 
                /// Get an iFolder User Information Object.
142
 
                /// </summary>
143
 
                /// <param name="member">The Member Object</param>
144
 
                /// <param name="collection">The Collection Object</param>
145
 
                /// <param name="domain">The Domain Object</param>
146
 
                /// <returns>An iFolderUser Object</returns>
147
 
                protected iFolderUser(Member member, Collection collection, Domain domain)
148
 
                {
149
 
                        this.ID = member.UserID;
150
 
                        this.UserName = member.Name;
151
 
            this.MemberRights = RightsUtility.Convert(member.Rights);
152
 
                        this.FullName = (member.FN != null) ? member.FN : member.Name;
153
 
                        this.FirstName = member.Given;
154
 
                        this.LastName = member.Family;
155
 
                        this.Enabled = !(domain.IsLoginDisabled(this.ID));
156
 
                        this.IsOwner = (member.UserID == collection.Owner.UserID);
157
 
                        this.Email = NodeUtility.GetStringProperty(member, EmailProperty);
158
 
 
159
 
                        if ( member.HomeServer != null )
160
 
                            this.HomeServer = (member.HomeServer.Name == null ) ? string.Empty : member.HomeServer.Name;
161
 
                        else 
162
 
                            this.HomeServer = string.Empty;
163
 
 
164
 
                        // NOTE: The member object may not be complete if it did not come from the
165
 
                        // domain object.
166
 
                        if (collection != domain)
167
 
                        {
168
 
                                Member domainMember = domain.GetMemberByID(this.ID);
169
 
                                this.FullName = (domainMember.FN != null) ? domainMember.FN : domainMember.Name;
170
 
                                this.FirstName = domainMember.Given;
171
 
                                this.LastName = domainMember.Family;
172
 
                                this.Email = NodeUtility.GetStringProperty(member, EmailProperty);
173
 
                        }
174
 
                }
175
 
 
176
 
                /// <summary>
177
 
                /// Update a User.
178
 
                /// </summary>
179
 
                /// <param name="userID">The User ID</param>
180
 
                /// <param name="user">The iFolderUser object with updated fields.</param>
181
 
                /// <param name="accessID">The Access User ID</param>
182
 
                /// <returns>An updated iFolderUser Object</returns>
183
 
                public static iFolderUser SetUser(string userID, iFolderUser user, string accessID)
184
 
                {
185
 
                        Store store = Store.GetStore();
186
 
 
187
 
                        Domain domain = store.GetDomain(store.DefaultDomain);
188
 
 
189
 
                        // impersonate
190
 
                        iFolder.Impersonate(domain, accessID);
191
 
 
192
 
                        Member member = domain.GetMemberByID(userID);
193
 
 
194
 
                        // check username also
195
 
                        if (member == null) member = domain.GetMemberByName(userID);
196
 
 
197
 
                        // not found
198
 
                        if (member == null) throw new UserDoesNotExistException(userID);
199
 
 
200
 
                        // update values
201
 
                        member.FN = user.FullName;
202
 
                        member.Given = user.FirstName;
203
 
                        member.Family = user.LastName;
204
 
                        member.Properties.ModifyProperty(EmailProperty, user.Email);
205
 
 
206
 
                        // no full name policy
207
 
                        if (((user.FullName == null) || (user.FullName.Length == 0))
208
 
                                && ((user.FirstName != null) && (user.FirstName.Length != 0))
209
 
                                && ((user.LastName != null) && (user.LastName.Length != 0)))
210
 
                        {
211
 
                                member.FN = String.Format("{0} {1}", user.FirstName, user.LastName);
212
 
                        }
213
 
 
214
 
                        // commit
215
 
                        domain.Commit(member);
216
 
 
217
 
                        return GetUser(userID, accessID);
218
 
                }
219
 
 
220
 
                /// <summary>
221
 
                /// Get a Member of an iFolder
222
 
                /// </summary>
223
 
                /// <param name="userID">The User ID</param>
224
 
                /// <param name="accessID">The Access User ID</param>
225
 
                /// <returns>An iFolderUser Object</returns>
226
 
                public static iFolderUser GetUser(string userID, string accessID)
227
 
                {
228
 
                        return GetUser(null, userID, accessID);
229
 
                }
230
 
 
231
 
                /// <summary>
232
 
                /// Get a Member of an iFolder
233
 
                /// </summary>
234
 
                /// <param name="ifolderID">The iFolder ID</param>
235
 
                /// <param name="userID">The User ID</param>
236
 
                /// <param name="accessID">The Access User ID</param>
237
 
                /// <returns>An iFolderUser Object</returns>
238
 
                public static iFolderUser GetUser(string ifolderID, string userID, string accessID)
239
 
                {
240
 
                        Store store = Store.GetStore();
241
 
 
242
 
                        Domain domain = store.GetDomain(store.DefaultDomain);
243
 
 
244
 
                        Collection c = null;
245
 
 
246
 
                        if (ifolderID == null)
247
 
                        {
248
 
                                // default to the domain
249
 
                                c = domain;
250
 
                        }
251
 
                        else
252
 
                        {
253
 
                                // get the collection
254
 
                                c = store.GetCollectionByID(ifolderID);
255
 
 
256
 
                                if (c == null) throw new iFolderDoesNotExistException(ifolderID);
257
 
                        }
258
 
                        
259
 
                        // impersonate
260
 
                        iFolder.Impersonate(c, accessID);
261
 
 
262
 
                        Member member = c.GetMemberByID(userID);
263
 
 
264
 
                        // check username also
265
 
                        if (member == null) member = c.GetMemberByName(userID);
266
 
 
267
 
                        // not found
268
 
                        if (member == null) throw new UserDoesNotExistException(userID);
269
 
 
270
 
                        // user
271
 
                        return new iFolderUser(member, c, domain);
272
 
                }
273
 
 
274
 
                /// <summary>
275
 
                /// Get the Members of an iFolder
276
 
                /// </summary>
277
 
                /// <param name="ifolderID">The iFolder ID</param>
278
 
                /// <param name="index">The Search Start Index</param>
279
 
                /// <param name="max">The Search Max Count of Results</param>
280
 
                /// <param name="accessID">The Access User ID</param>
281
 
                /// <returns>An iFolder User Set</returns>
282
 
                public static iFolderUserSet GetUsers(string ifolderID, int index, int max, string accessID)
283
 
                {
284
 
                        Store store = Store.GetStore();
285
 
 
286
 
            Domain domain = store.GetDomain(store.DefaultDomain);
287
 
 
288
 
            Collection c = null;
289
 
 
290
 
                        if (ifolderID == null)
291
 
                        {
292
 
                                // default to the domain
293
 
                                c = domain;
294
 
                        }
295
 
                        else
296
 
                        {
297
 
                                // get the collection
298
 
                                c = store.GetCollectionByID(ifolderID);
299
 
 
300
 
                                if (c == null) throw new iFolderDoesNotExistException(ifolderID);
301
 
                        }
302
 
 
303
 
                        // impersonate
304
 
                        iFolder.Impersonate(c, accessID);
305
 
 
306
 
                        // members
307
 
                        ICSList members = c.GetMemberList();
308
 
                        
309
 
                        // sort the list
310
 
                        ArrayList sortList = new ArrayList();
311
 
                        
312
 
                        foreach(ShallowNode sn in members)
313
 
                        {
314
 
                                sortList.Add(sn);
315
 
                        }
316
 
                        
317
 
                        sortList.Sort();
318
 
 
319
 
                        // build the result list
320
 
                        ArrayList list = new ArrayList();
321
 
                        int i = 0;
322
 
 
323
 
                        foreach(ShallowNode sn in sortList)
324
 
                        {
325
 
                                Member member = new Member(c, sn);
326
 
 
327
 
                                // Don't include the Host objects as iFolder users.
328
 
                                if (!member.IsType("Host"))
329
 
                                {
330
 
                                        if ((i >= index) && (((max <= 0) || i < (max + index))))
331
 
                                        {
332
 
                                                Member tmpmember = domain.GetMemberByID(member.UserID);
333
 
                                                if(tmpmember!= null)
334
 
                                                        list.Add(new iFolderUser(member, c, domain));
335
 
                                        }
336
 
 
337
 
                                        ++i;
338
 
                                }
339
 
                        }
340
 
 
341
 
                        return new iFolderUserSet((iFolderUser[])list.ToArray(typeof(iFolderUser)), i);
342
 
                }
343
 
 
344
 
                /// <summary>
345
 
                /// Get Users by Search
346
 
                /// </summary>
347
 
                /// <param name="property">The Search Property</param>
348
 
                /// <param name="operation">The Search Operator</param>
349
 
                /// <param name="pattern">The Search Pattern</param>
350
 
                /// <param name="index">The Search Start Index</param>
351
 
                /// <param name="max">The Search Max Count of Results</param>
352
 
                /// <param name="accessID">The Access User ID</param>
353
 
                /// <returns>An iFolder User Set</returns>
354
 
                public static iFolderUserSet GetUsers(SearchProperty property, SearchOperation operation, string pattern, int index, int max, string accessID)
355
 
                {
356
 
                        Store store = Store.GetStore();
357
 
 
358
 
                        Domain domain = store.GetDomain(store.DefaultDomain);
359
 
 
360
 
                        // search operator
361
 
                        SearchOp searchOperation;
362
 
 
363
 
                        switch(operation)
364
 
                        {
365
 
                                case SearchOperation.BeginsWith:
366
 
                                        searchOperation = SearchOp.Begins;
367
 
                                        break;
368
 
 
369
 
                                case SearchOperation.EndsWith:
370
 
                                        searchOperation = SearchOp.Ends;
371
 
                                        break;
372
 
 
373
 
                                case SearchOperation.Contains:
374
 
                                        searchOperation = SearchOp.Contains;
375
 
                                        break;
376
 
 
377
 
                                case SearchOperation.Equals:
378
 
                                        searchOperation = SearchOp.Equal;
379
 
                                        break;
380
 
 
381
 
                                default:
382
 
                                        searchOperation = SearchOp.Contains;
383
 
                                        break;
384
 
                        }
385
 
                        
386
 
                        // search property
387
 
                        string searchProperty;
388
 
 
389
 
                        switch(property)
390
 
                        {
391
 
                                case SearchProperty.UserName:
392
 
                                        searchProperty = BaseSchema.ObjectName;
393
 
                                        break;
394
 
 
395
 
                                case SearchProperty.FullName:
396
 
                                        searchProperty = PropertyTags.FullName;
397
 
                                        break;
398
 
 
399
 
                                case SearchProperty.LastName:
400
 
                                        searchProperty = PropertyTags.Family;
401
 
                                        break;
402
 
 
403
 
                                case SearchProperty.FirstName:
404
 
                                        searchProperty = PropertyTags.Given;
405
 
                                        break;
406
 
 
407
 
                                default:
408
 
                                        searchProperty = PropertyTags.FullName;
409
 
                                        break;
410
 
                        }
411
 
                        
412
 
                        // impersonate
413
 
                        iFolder.Impersonate(domain, accessID);
414
 
 
415
 
                        // create the search list
416
 
                        ICSList searchList = domain.Search(searchProperty, pattern, searchOperation);
417
 
                        
418
 
                        // build the result list
419
 
                        ArrayList list = new ArrayList();
420
 
                        int i = 0;
421
 
 
422
 
                        foreach(ShallowNode sn in searchList)
423
 
                        {
424
 
                                if (sn.IsBaseType(NodeTypes.MemberType))
425
 
                                {
426
 
                                        Member member = new Member(domain, sn);
427
 
 
428
 
                                        // Don't include Host objects as iFolder users.
429
 
                                        if (!member.IsType("Host"))
430
 
                                        {
431
 
                                                if ((i >= index) && (((max <= 0) || i < (max + index))))
432
 
                                                {
433
 
                                                        list.Add(new iFolderUser(member, domain, domain));
434
 
                                                }
435
 
 
436
 
                                                ++i;
437
 
                                        }
438
 
                                }
439
 
                        }
440
 
 
441
 
                        return new iFolderUserSet((iFolderUser[])list.ToArray(typeof(iFolderUser)), i);
442
 
                }
443
 
 
444
 
                /// <summary>
445
 
                /// Set the iFolder Rights of a Member
446
 
                /// </summary>
447
 
                /// <param name="ifolderID">The iFolder ID</param>
448
 
                /// <param name="userID">The Member User ID</param>
449
 
                /// <param name="rights">The New Rights</param>
450
 
                /// <param name="accessID">The Access User ID</param>
451
 
                public static void SetMemberRights(string ifolderID, string userID, Rights rights, string accessID)
452
 
                {
453
 
                        try
454
 
                        {
455
 
                                SharedCollection.SetMemberRights(ifolderID, userID, RightsUtility.Convert(rights).ToString(), accessID);
456
 
                        }
457
 
                        catch(CollectionStoreException e)
458
 
                        {
459
 
                                if (e.Message.IndexOf("change owner's rights") != -1)
460
 
                                {
461
 
                                        throw new InvalidOperationException("The rights of the owner of the iFolder can not be changed.", e);
462
 
                                }
463
 
 
464
 
                                throw;
465
 
                        }
466
 
                }
467
 
 
468
 
                /// <summary>
469
 
                /// Add a Member to an iFolder
470
 
                /// </summary>
471
 
                /// <param name="ifolderID">The iFolder ID</param>
472
 
                /// <param name="userID">The User ID</param>
473
 
                /// <param name="rights">The New Rights</param>
474
 
                /// <param name="accessID">The Access User ID</param>
475
 
                public static void AddMember(string ifolderID, string userID, Rights rights, string accessID)
476
 
                {
477
 
                        try
478
 
                        {
479
 
                                SharedCollection.AddMember(ifolderID, userID, RightsUtility.Convert(rights).ToString(),
480
 
                                        iFolder.iFolderCollectionType, accessID);
481
 
                        }
482
 
                        catch(ExistsException)
483
 
                        {
484
 
                                // ignore an already exists exception
485
 
                        }
486
 
                }
487
 
 
488
 
                /// <summary>
489
 
                /// Remove a Member from an iFolder
490
 
                /// </summary>
491
 
                /// <param name="ifolderID">The iFolder ID</param>
492
 
                /// <param name="userID">The User ID</param>
493
 
                /// <param name="accessID">The Access User ID</param>
494
 
                public static void RemoveMember(string ifolderID, string userID, string accessID)
495
 
                {
496
 
                        try
497
 
                        {
498
 
                                SharedCollection.RemoveMember(ifolderID, userID, accessID);
499
 
                        }
500
 
                        catch(Exception e)
501
 
                        {
502
 
                                // guess and improve exception
503
 
                                if (e.Message.IndexOf("iFolder owner") != -1)
504
 
                                {
505
 
                                        throw new InvalidOperationException("The owner of an iFolder can not be removed.", e);
506
 
                                }
507
 
 
508
 
                                throw;
509
 
                        }
510
 
                }
511
 
 
512
 
                /// <summary>
513
 
                /// Se the Owner of an iFolder
514
 
                /// </summary>
515
 
                /// <param name="ifolderID">The iFolder ID</param>
516
 
                /// <param name="userID">The User ID</param>
517
 
                /// <param name="accessID">The Access User ID</param>
518
 
                public static void SetOwner(string ifolderID, string userID, string accessID, bool OrphanAdopt)
519
 
                {
520
 
                        try
521
 
                        {
522
 
                                // check that the member exists
523
 
                                GetUser(ifolderID, userID, accessID);
524
 
                        }
525
 
                        catch
526
 
                        {
527
 
                                // member does not exist
528
 
                                AddMember(ifolderID, userID, Rights.Admin, accessID);
529
 
                        }
530
 
 
531
 
                        // note: default the previous owner to "ReadOnly" rights
532
 
                        SharedCollection.ChangeOwner(ifolderID, userID, Access.Rights.ReadOnly.ToString(), accessID);
533
 
                        
534
 
                        //If orphaned collection was adopted then delete the 'OrphanedOwner' property
535
 
                        if(OrphanAdopt)
536
 
                        {
537
 
                                Store store = Store.GetStore();
538
 
                                Collection c = store.GetCollectionByID(ifolderID);
539
 
                                if (c == null)  throw new iFolderDoesNotExistException(ifolderID);
540
 
                                Property p = c.Properties.GetSingleProperty( "OrphanedOwner" );
541
 
                                if ( p != null )
542
 
                                {
543
 
                                        c.Properties.DeleteSingleProperty( "OrphanedOwner" );
544
 
                                        c.Commit();
545
 
                                }
546
 
                        }       
547
 
                }
548
 
 
549
 
                /// <summary>
550
 
                /// Is the User an Administrator
551
 
                /// </summary>
552
 
                /// <remarks>
553
 
                /// A User is a system administrator if the user has "Admin" rights in the domain.
554
 
                /// </remarks>
555
 
                /// <param name="userID">The User ID</param>
556
 
                /// <returns>true, if the User is a System Administrator</returns>
557
 
                public static bool IsAdministrator(string userID)
558
 
                {
559
 
                        Store store = Store.GetStore();
560
 
 
561
 
                        Domain domain = store.GetDomain(store.DefaultDomain);
562
 
                        Member member = domain.GetMemberByID( userID );
563
 
                        Access.Rights rights = (member != null) ? member.Rights : Access.Rights.Deny;
564
 
                        
565
 
                        return (rights == Access.Rights.Admin);
566
 
                }
567
 
 
568
 
                /// <summary>
569
 
                /// Give a User Administration Rights
570
 
                /// </summary>
571
 
                /// <remarks>
572
 
                /// A User is a system administrator if the user has "Admin" rights in the domain.
573
 
                /// </remarks>
574
 
                /// <param name="userID">The User ID</param>
575
 
                public static void AddAdministrator(string userID)
576
 
                {
577
 
                        Store store = Store.GetStore();
578
 
 
579
 
                        Domain domain = store.GetDomain(store.DefaultDomain);
580
 
 
581
 
                        SetMemberRights(domain.ID, userID, Rights.Admin, null);
582
 
                }
583
 
 
584
 
                /// <summary>
585
 
                /// Remove Administration Rights from a User
586
 
                /// </summary>
587
 
                /// <remarks>
588
 
                /// Administration rights are removed by giving the user "ReadOnly" rights in the domain.
589
 
                /// </remarks>
590
 
                /// <param name="userID">The User ID</param>
591
 
                public static void RemoveAdministrator(string userID)
592
 
                {
593
 
                        Store store = Store.GetStore();
594
 
 
595
 
                        Domain domain = store.GetDomain(store.DefaultDomain);
596
 
 
597
 
                        SetMemberRights(domain.ID, userID, Rights.ReadOnly, null);
598
 
                }
599
 
 
600
 
                /// <summary>
601
 
                /// Get the Administrators
602
 
                /// </summary>
603
 
                /// <param name="index">The Search Start Index</param>
604
 
                /// <param name="max">The Search Max Count of Results</param>
605
 
                /// <remarks>
606
 
                /// A User is a system administrator if the user has "Admin" rights in the domain.
607
 
                /// </remarks>
608
 
                /// <returns>An iFolder User Set</returns>
609
 
                public static iFolderUserSet GetAdministrators(int index, int max)
610
 
                {
611
 
                        Store store = Store.GetStore();
612
 
 
613
 
                        Domain domain = store.GetDomain(store.DefaultDomain);
614
 
 
615
 
                        ICSList members = domain.GetMembersByRights(Access.Rights.Admin);
616
 
                        
617
 
                        // sort the list
618
 
                        ArrayList sortList = new ArrayList();
619
 
                        
620
 
                        foreach(ShallowNode sn in members)
621
 
                        {
622
 
                                sortList.Add(sn);
623
 
                        }
624
 
                        
625
 
                        sortList.Sort();
626
 
 
627
 
                        // build the result list
628
 
                        ArrayList list = new ArrayList();
629
 
                        int i = 0;
630
 
 
631
 
                        foreach(ShallowNode sn in sortList)
632
 
                        {
633
 
                                Member member = new Member(domain, sn);
634
 
 
635
 
                                // Don't include Host objects as iFolder administrators.
636
 
                                if (!member.IsType("Host"))
637
 
                                {
638
 
                                        if ((i >= index) && (((max <= 0) || i < (max + index))))
639
 
                                        {
640
 
                                                list.Add(new iFolderUser(member, domain, domain));
641
 
                                        }
642
 
 
643
 
                                        ++i;
644
 
                                }
645
 
                        }
646
 
 
647
 
                        return new iFolderUserSet((iFolderUser[])list.ToArray(typeof(iFolderUser)), i);
648
 
                }
649
 
        
650
 
 
651
 
                ///<summary>
652
 
                ///</summary>
653
 
                ///<returns></returns>
654
 
                public static bool IsPassPhraseSet (string DomainID, string AccessID)
655
 
                {
656
 
 
657
 
                        string  CryptoKeyBlob = null;
658
 
                        try
659
 
                        {
660
 
                                Store store = Store.GetStore();
661
 
                                                        
662
 
                                Collection collection = store.GetCollectionByID(DomainID);
663
 
                                Simias.Storage.Member member = collection.GetMemberByID(AccessID);
664
 
                                
665
 
                                CryptoKeyBlob =  member.ServerGetPassKeyHash();
666
 
                        }
667
 
                        catch(Exception ex)
668
 
                        {
669
 
                                log.Debug("IsPassPhraseSet : {0}", ex.Message);
670
 
                                throw ex;
671
 
                        }
672
 
                        log.Debug("IsPassPhraseSet :{0}", CryptoKeyBlob);
673
 
                        if(CryptoKeyBlob == String.Empty)
674
 
                        {
675
 
                                log.Debug("IsPassPhraseSet : false");
676
 
                                return false;
677
 
                        }
678
 
                        else    
679
 
                        {
680
 
                                log.Debug("IsPassPhraseSet : true");
681
 
                                return true;
682
 
                        }
683
 
                }
684
 
                
685
 
                
686
 
                ///<summary>
687
 
                ///Validate the passphrase for the correctness
688
 
                ///</summary>
689
 
                ///<returns>passPhrase.</returns>
690
 
                public static Simias.Authentication.Status ValidatePassPhrase(string DomainID, string Passphrase, string AccessID)
691
 
                {
692
 
                        string OldHash = null;
693
 
                        string NewHash = null;
694
 
                        
695
 
                        try
696
 
                        {
697
 
                                Store store = Store.GetStore();
698
 
                                        
699
 
                                Collection collection = store.GetCollectionByID(DomainID);
700
 
                                Simias.Storage.Member member = collection.GetMemberByID(AccessID);
701
 
                                
702
 
                                log.Debug("Member ValidatePassPhrase User:{0}...{1} ", member.Name, member.UserID);
703
 
                                
704
 
                                log.Debug("ValidatePassPhrase : got PassKey");
705
 
                                string EncrypCryptoKey = member.ServerGetEncrypPassKey();
706
 
                                log.Debug("ValidatePassPhrase : got PassKey:{0}",EncrypCryptoKey);
707
 
 
708
 
                                //Hash the passphrase and use it for encryption and decryption
709
 
                                PassphraseHash hash = new PassphraseHash();
710
 
                                byte[] passphrase = hash.HashPassPhrase(Passphrase);    
711
 
                        
712
 
                                //Decrypt it
713
 
                                string DecryptedCryptoKey; 
714
 
                                Key DeKey = new Key(EncrypCryptoKey);
715
 
                                DeKey.DecrypytKey(passphrase, out DecryptedCryptoKey);
716
 
 
717
 
                                //Encrypt using passphrase
718
 
                                string EncryptedCryptoKey;
719
 
                                Key EnKey = new Key(DecryptedCryptoKey);
720
 
                                EnKey.EncrypytKey(passphrase, out EncryptedCryptoKey);
721
 
 
722
 
                                //SHA1
723
 
                                Key HashKey = new Key(EncryptedCryptoKey);
724
 
                                NewHash = HashKey.HashKey();
725
 
 
726
 
                                OldHash = member.ServerGetPassKeyHash();
727
 
                                log.Debug("ValidatePassPhrase : getting OldHash:{0}", OldHash);
728
 
                        }
729
 
                        catch(Exception ex)
730
 
                        {
731
 
                                log.Debug("ValidatePassPhrase : {0}", ex.Message);
732
 
                                throw ex;
733
 
                        }
734
 
                        
735
 
                        //Compare
736
 
                        log.Debug("ValidatePassPhrase : Comparing blobs {0}...{1}",OldHash, NewHash);
737
 
                        if(String.Equals(OldHash, NewHash)==true)
738
 
                        {
739
 
                                log.Debug("ValidatePassPhrase : true");
740
 
                                return new Simias.Authentication.Status(Simias.Authentication.StatusCodes.Success);
741
 
                        }
742
 
                        else    
743
 
                        {
744
 
                                log.Debug("ValidatePassPhrase : false");                        
745
 
                                return new Simias.Authentication.Status(Simias.Authentication.StatusCodes.PassPhraseInvalid);
746
 
                        }
747
 
                }
748
 
                
749
 
                ///<summary>
750
 
                ///Set the passphrase and recovery agent
751
 
                ///</summary>
752
 
                ///<returns>passPhrase.</returns>
753
 
                public static void SetPassPhrase(string DomainID, string Passphrase, string RAName, string RAPublicKey, string AccessID)
754
 
                {
755
 
                        try
756
 
                        {
757
 
                                if(RAPublicKey != null && RAPublicKey != "" && RAName != null && RAName != "")
758
 
                                {
759
 
                                        byte [] key = Convert.FromBase64String(RAPublicKey);
760
 
                                        if(key.Length > 64 && key.Length < 128) //remove the 5 byte header and 5 byte trailer
761
 
                                        {
762
 
                                                byte[] NewKey = new byte[key.Length-10];
763
 
                                                Array.Copy(key, 5, NewKey, 0, key.Length-10);
764
 
                                                RAPublicKey = Convert.ToBase64String(NewKey);
765
 
                                        }
766
 
                                        else if(key.Length > 128 && key.Length < 256) //remove the 7 byte header and 5 byte trailer
767
 
                                        {
768
 
                                                byte[] NewKey = new byte[key.Length-12];
769
 
                                                Array.Copy(key, 7, NewKey, 0, key.Length-12);
770
 
                                                RAPublicKey = Convert.ToBase64String(NewKey);
771
 
                                        }                                       
772
 
                                        else if(key.Length > 256) //remove the 9 byte header and 5 byte trailer
773
 
                                        {
774
 
                                                byte[] NewKey = new byte[key.Length-14];
775
 
                                                Array.Copy(key, 9, NewKey, 0, key.Length-14);
776
 
                                                RAPublicKey = Convert.ToBase64String(NewKey);
777
 
                                        }                                       
778
 
                                        else
779
 
                                        {
780
 
                                                log.Debug("KeyCorrection RAName: {0}", RAName);
781
 
                                                log.Debug("KeyCorrection RAPublicKey: {0}", RAPublicKey);
782
 
                                                log.Debug("KeyCorrection key.Length: {0}", key.Length);
783
 
                                                throw new SimiasException("Recovery key size not suported");
784
 
                                        }
785
 
                                }
786
 
                
787
 
                
788
 
                        
789
 
                                Store store = Store.GetStore();
790
 
                                                                
791
 
                                Collection collection = store.GetCollectionByID(DomainID);
792
 
                                Simias.Storage.Member member = collection.GetMemberByID(AccessID);
793
 
 
794
 
                                //Hash the passphrase and use it for encryption and decryption
795
 
                                PassphraseHash hash = new PassphraseHash();
796
 
                                byte[] passphrase = hash.HashPassPhrase(Passphrase);    
797
 
                                
798
 
                                Key RAkey = new Key((passphrase.Length)*8);
799
 
                                string EncrypCryptoKey;
800
 
                                RAkey.EncrypytKey(passphrase, out EncrypCryptoKey);
801
 
                                Key HashKey = new Key(EncrypCryptoKey);
802
 
                                
803
 
                                log.Debug("SetPassPhrase {0}...{1}...{2}...{3}",EncrypCryptoKey, HashKey.HashKey(), RAName, RAPublicKey);
804
 
                                member.ServerSetPassPhrase(EncrypCryptoKey, HashKey.HashKey(), RAName, RAPublicKey);
805
 
                        }
806
 
                        catch(Exception ex)
807
 
                        {
808
 
                                log.Debug("SetPassPhrase : {0}", ex.Message);
809
 
                                //throw ex;
810
 
                        }
811
 
                }               
812
 
        }
813
 
}