Skip to content

Commit

Permalink
Merge pull request #253 from Blazam-App/Beta-Dev
Browse files Browse the repository at this point in the history
Fix for new users created as disabled
  • Loading branch information
jacobsen9026 authored Feb 27, 2024
2 parents 38d9a12 + bdb10b9 commit 45b6453
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 13 deletions.
2 changes: 1 addition & 1 deletion BLAZAM/BLAZAM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<ServerGarbageCollection>false</ServerGarbageCollection>
<AssemblyVersion>0.8.9</AssemblyVersion>
<Version>2024.02.26.0341</Version>
<Version>2024.02.27.2343</Version>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<RootNamespace>BLAZAM</RootNamespace>
<GenerateDocumentationFile>False</GenerateDocumentationFile>
Expand Down
7 changes: 5 additions & 2 deletions BLAZAMActiveDirectory/ActiveDirectoryContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public bool PortOpen
}
private DirectoryConnectionStatus _status = DirectoryConnectionStatus.Connecting;
private IApplicationUserState? currentUser;
private IADUser? _keepAliveUser;

public DirectoryConnectionStatus Status
{
Expand Down Expand Up @@ -202,7 +203,6 @@ INotificationPublisher notificationPublisher
UserStateService = userStateService;
//UserStateService.UserStateAdded += PopulateUserStateDirectoryUser;
ConnectAsync();
_timer = new Timer(KeepAlive, null, 30000, 30000);

Users = new ADUserSearcher(this);
Groups = new ADGroupSearcher(this);
Expand Down Expand Up @@ -257,7 +257,7 @@ private async void KeepAlive(object? state)
else if (Status == DirectoryConnectionStatus.OK)
{
//Throw away query used to keep connection alive
_ = Users?.FindUsersByString(ConnectionSettings?.Username, false)?.FirstOrDefault();
_keepAliveUser = Users?.FindUsersByString(ConnectionSettings?.Username, false)?.FirstOrDefault();
}
}

Expand Down Expand Up @@ -296,6 +296,9 @@ public void Connect()
//No reason connecting if we're already connected
if (Status != DirectoryConnectionStatus.OK)
{
_timer?.Dispose();
_timer = new Timer(KeepAlive, null, 0, 30000);

//Ok get the latest settings
ADSettings? ad = Context?.ActiveDirectorySettings.FirstOrDefault();
ConnectionSettings = ad;
Expand Down
1 change: 1 addition & 0 deletions BLAZAMActiveDirectory/Adapters/ADOrganizationalUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ public IADUser CreateUser(string containerName)
DirectoryEntry = searchResult?.GetDirectoryEntry();
newUser.Parse(directoryEntry: DirectoryEntry.Children.Add(fullContainerName, "user"), directory: Directory);
newUser.NewEntry = true;
newUser.Enabled = true;
return newUser;
}catch(Exception ex)
{
Expand Down
50 changes: 44 additions & 6 deletions BLAZAMActiveDirectory/Adapters/AccountDirectoryAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using BLAZAM.Jobs;
using BLAZAM.Logger;
using System.Data;
using System.Diagnostics;
using System.DirectoryServices.AccountManagement;
using System.Globalization;
using System.Security;
Expand All @@ -19,6 +20,8 @@ public class AccountDirectoryAdapter : GroupableDirectoryAdapter, IAccountDirect
const int ADS_UF_PASSWD_CANT_CHANGE = 0x0040;
const int ADS_UF_NORMAL_ACCOUNT = 0x0200;
const int ADS_UF_DONT_EXPIRE_PASSWD = 0x10000;
const int PASSWD_NOTREQD_MASK = 0xFFDF;

const int ACCOUNT_ENABLE_MASK = 0xFFFFFFD;


Expand Down Expand Up @@ -118,21 +121,54 @@ public virtual bool Disabled
}
}
}
public virtual bool PasswordNotRequired
{
get
{

try
{
return (UAC & ADS_UF_PASSWD_NOTREQD) == ADS_UF_PASSWD_NOTREQD;
}
catch
{
// handle NullReferenceException
}
return true;
}
set
{
if (value && !PasswordNotRequired)
{
UAC = UAC | ADS_UF_PASSWD_NOTREQD;
}
else if (!value && PasswordNotRequired)
{

UAC = UAC & PASSWD_NOTREQD_MASK;

}
}
}
protected int UAC
{
get
{
var uacRaw= Convert.ToInt32(GetProperty<object>("userAccountControl"));
if(uacRaw == 0)
{
return 546;
UAC = ADS_UF_NORMAL_ACCOUNT | ADS_UF_PASSWD_NOTREQD;
return ADS_UF_NORMAL_ACCOUNT | ADS_UF_PASSWD_NOTREQD;
}
return uacRaw;
}
set
{
SetProperty("userAccountControl", value);
// PostCommitSteps.Add(new("Set UAC", (step) => {
SetProperty("userAccountControl", value);

// return true;
// }));
}
}

Expand Down Expand Up @@ -244,7 +280,8 @@ public bool SetPassword(SecureString password, bool requireChange = false)
up.SetPassword(password.ToPlainText());
if (requireChange)
up.ExpirePasswordNow();

if(NewEntry)
up.PasswordNotRequired = false;
up.Save();

}
Expand All @@ -257,10 +294,11 @@ public bool SetPassword(SecureString password, bool requireChange = false)
{

Loggers.ActiveDirectryLogger.Error("Error setting entry password {@Error}", ex);

throw new ApplicationException("Unable to set password", ex);
if (!Debugger.IsAttached)
throw new ApplicationException("Unable to set password", ex);
else return true;
}

}

public void StagePasswordChange(SecureString newPassword, bool requireChange = false)
Expand Down
2 changes: 1 addition & 1 deletion BLAZAMActiveDirectory/Adapters/DirectoryEntryAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public virtual void MoveTo(IADOrganizationalUnit parentOUToMoveTo)
HasUnsavedChanges = true;
}

public virtual string? OU { get => DirectoryTools.DnToOu(DN); }
public virtual string? OU { get => DirectoryTools.DnToOu(DN)??DirectoryTools.DnToOu(ADSPath); }

public IADOrganizationalUnit? GetParent()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public interface IAccountDirectoryAdapter : IGroupableDirectoryAdapter
/// If a password change is staged using <see cref="StagePasswordChange(SecureString, bool)"/>, holds the encrypted new password to be applied.
/// </summary>
SecureString? NewPassword { get; set; }
bool PasswordNotRequired { get; set; }

/// <summary>
/// Changes the password for this entry immediately
Expand Down
3 changes: 2 additions & 1 deletion BLAZAMGui/UI/Modals/AppNewsItemDialog.razor
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@
CurrentUser.State.ReadNewsItems.Add(new ReadNewsItem { NewsItemId = Item.Id, NewsItemUpdatedAt = Item.UpdatedAt, User = CurrentUser.State.Preferences });

}
await CurrentUser.State.SaveUserSettings();
MudDialog.Close(DialogResult.Ok(true));

await CurrentUser.State.SaveUserSettings();
OnAcknowledged?.Invoke();
}
}
7 changes: 5 additions & 2 deletions BLAZAMJobs/JobStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ public class JobStep : JobStepBase, IJobStep
public Func<JobStep?, Task<bool>>? AsyncAction { get; }


public JobStep(string name, Func<JobStep?, bool> action)
public JobStep(string name, Func<JobStep?, bool> action,bool stopOnError=false)
{
StopOnFailedStep = stopOnError;
Name = name;
Action = action;
}
public JobStep(string name, Func<JobStep?, Task<bool>> asyncAction)
public JobStep(string name, Func<JobStep?, Task<bool>> asyncAction, bool stopOnError = false)
{
StopOnFailedStep = stopOnError;

Name = name;
AsyncAction = asyncAction;
}
Expand Down

0 comments on commit 45b6453

Please sign in to comment.