Advertisement
Guest User

Untitled

a guest
Sep 13th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. public override Object Invoke( UserSession session, Object inputParams )
  2. {
  3. string User = "g.сhernyshev";
  4. string Password = "123";
  5. string DomainName = "TEST";
  6. string ADGroupName = "test";
  7. Template UserTemplate = Service.GetCommonRegistryValue<Template>( @"CommonSettings\plmsoyuzconfig\Configurations\ADSYNC\UserTemplate" );
  8. string Login = Service.GetCommonRegistryValue<string>( @"CommonSettings\plmsoyuzconfig\Configurations\ADSYNC\AccountId", "sAMAccountName" );
  9. string UserFlags = Service.GetCommonRegistryValue<string>( @"CommonSettings\plmsoyuzconfig\Configurations\ADSYNC\UserFlags", "userAccountControl" );
  10.  
  11. var Params = Service.GetRegistryItem( null, @"CommonSettings\plmsoyuzconfig\Configurations\ADSYNC\Params", null, RegistryItemBase.DataTypeEnum.Undefined, false, true );
  12. var ParamsArray = new Dictionary<string, string>();
  13. foreach( var i in Params.AllChildren )
  14. {
  15. ParamsArray.Add( i.NameKey, i.GetValue<string>() );
  16. }
  17.  
  18. System.DirectoryServices.DirectoryEntry rootEnt = null;
  19. System.DirectoryServices.DirectorySearcher DirSearch = null;
  20. var allPLMUsers = Service.AllUsers.Where( u => u.IsUser ).ToArray();
  21.  
  22. rootEnt = new System.DirectoryServices.DirectoryEntry("LDAP://" + DomainName, User, Password );
  23.  
  24. if( rootEnt != null )
  25. {
  26. System.DirectoryServices.DirectoryEntry ADUser = new System.DirectoryServices.DirectoryEntry( "LDAP://" + DomainName + "/", User, Password );
  27.  
  28. var sAMAccountName = ( ADUser.Properties[ Login ].Count > 0 ) ? ADUser.Properties[ Login ][0].ToString() : "";
  29. if( !string.IsNullOrEmpty( sAMAccountName ) )
  30. {
  31. var sAMAccountNameKey = sAMAccountName.Replace( '.', '_' ).Replace( ',', '_' ).Replace( ';', '_' ).Replace( '\\', '_' ).Replace( '&', '_' ).Replace( '*', '_' ).Replace( '/', '_' );
  32. var user = allPLMUsers.FirstOrDefault( u => u.AccountId.ToLower() == sAMAccountName.ToLower() );
  33. if( user == null || ( user != null && !user.GetValue<bool>( "NotAllowedSync", false ) ) )
  34. {
  35. if( user == null )
  36. {
  37. user = new User( UserTemplate );
  38. user.AccountId = sAMAccountName;
  39. user.NameKey = sAMAccountNameKey;
  40. user.Kind = UserBase.UserKindEnum.InternalUser;
  41. user.Notes = string.Format( Owner.GetLocalized( "importedFrom" , "Учетная запись импортирована из домена {0}" ), DomainName );
  42. }
  43. if( user.GetValue<string>( "Login" ) != sAMAccountName )
  44. user[ "Login" ] = sAMAccountName;
  45.  
  46. var SID = new System.Security.Principal.SecurityIdentifier( ( byte[] )ADUser.Properties[ "objectSid" ][0], 0 ).Value;
  47. if( user.SID != SID )
  48. user.SID = SID;
  49.  
  50. var ADName = string.Format( "{0} ({1})", sAMAccountName, SID );
  51. if( user.GetValue<string>( "ADName" ) != ADName )
  52. user[ "ADName" ] = ADName;
  53.  
  54. user[ "FirstName" ] = "Георгий";
  55. user[ "SecondName" ] = "Чернышов";
  56. user[ "MiddleName" ] = "Владимирович";
  57.  
  58. int userAccountControl = ( ADUser.Properties[ UserFlags ].Count > 0 ) ? ( int )ADUser.Properties[ UserFlags ][0].GetValueOfType<int>() : 0;
  59. bool IsDisabled = ( ( userAccountControl & 0x0002 ) > 0 );
  60. if( IsDisabled != user.IsDisabled )
  61. user[ "IsDisabled" ] = IsDisabled;
  62.  
  63. foreach( var param in ParamsArray )
  64. {
  65. var val = ( ADUser.Properties[ param.Key ].Count > 0 ) ? ADUser.Properties[ param.Key ][0].ToString() : "";
  66. if( user.GetValue<string>( param.Value, "" ) != val )
  67. user[ param.Value ] = val;
  68. }
  69.  
  70. }
  71. }
  72. }
  73. return null;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement