Codeunit 138049 User Login Test Library

System Application Test Library, System.TestLibraries.Security.AccessControl

src/System Application/Test Library/User Login Times/src/UserLoginTestLibrary.Codeunit.al102 lines, Copyright (c) Microsoft Corporation. MIT

// ------------------------------------------------------------------------------------------------// Copyright (c) Microsoft Corporation. All rights reserved.// Licensed under the MIT License. See License.txt in the project root for license information.// ------------------------------------------------------------------------------------------------namespace System.TestLibraries.Security.AccessControl;using System.Security.AccessControl;using System.Security.User;codeunit 138049 "User Login Test Library"{    Permissions = tabledata "User Environment Login" = rid,                  tabledata "User Login" = rimd,                  tabledata User = rd;    /// <summary>    /// Creates login information for a user.    /// </summary>    /// <param name="UserSID">The security ID of the user for whom to create the login information</param>    /// <param name="FirstLoginDate">Date to be entered as first login</param>    /// <param name="LastLoginDateTime">Date time to be entered as last login</param>    /// <param name="PenultimateLoginDateTime">Date time to be entered as penultimate login</param>    procedure InsertUserLogin(UserSID: Guid; FirstLoginDate: Date; LastLoginDateTime: DateTime; PenultimateLoginDateTime: DateTime)    var        UserLogin: Record "User Login";        UserEnvironmentLogin: Record "User Environment Login";    begin        UserLogin."User SID" := UserSID;        UserLogin."First Login Date" := FirstLoginDate;        UserLogin."Last Login Date" := LastLoginDateTime;        UserLogin."Penultimate Login Date" := PenultimateLoginDateTime;        UserLogin.Insert();        UserEnvironmentLogin."User SID" := UserSID;        UserEnvironmentLogin.Insert();    end;    /// <summary>    /// Updates login information for a user.    /// </summary>    /// <param name="UserSID">The security ID of the user for whom to update the login information</param>    /// <param name="FirstLoginDate">Date to be entered as first login</param>    /// <param name="LastLoginDateTime">Date time to be entered as last login</param>    /// <param name="PenultimateLoginDateTime">Date time to be entered as penultimate login</param>    procedure UpdateUserLogin(UserSID: Guid; FirstLoginDate: Date; LastLoginDateTime: DateTime; PenultimateLoginDateTime: DateTime)    var        UserLogin: Record "User Login";    begin        UserLogin.Get(UserSID);        UserLogin."First Login Date" := FirstLoginDate;        UserLogin."Last Login Date" := LastLoginDateTime;        UserLogin."Penultimate Login Date" := PenultimateLoginDateTime;        UserLogin.Modify();    end;    /// <summary>    /// Deletes all login information for a user.    /// </summary>    /// <param name="UserSID">The security ID of the user for whom to delete the login information</param>    procedure DeleteAllLoginInformation(UserSID: Guid)    var        UserLogin: Record "User Login";        UserEnvironmentLogin: Record "User Environment Login";    begin        UserLogin.SetRange("User SID", UserSID);        UserLogin.DeleteAll();        UserEnvironmentLogin.SetRange("User SID", UserSID);        UserEnvironmentLogin.DeleteAll();    end;    /// <summary>    /// Deletes all login information for all users.    /// </summary>    procedure DeleteAllLoginInformation()    var        UserLogin: Record "User Login";        UserEnvironmentLogin: Record "User Environment Login";    begin        UserLogin.DeleteAll();        UserEnvironmentLogin.DeleteAll();    end;    /// <summary>    /// Deletes all users that are not logged in yet.    /// </summary>    procedure DeleteAllUnLoggedInUsers()    var        User: Record User;        UserLoginTimeTracker: Codeunit "User Login Time Tracker";    begin        if User.FindSet() then            repeat                if not UserLoginTimeTracker.UserLoggedInEnvironment(User."User Security ID") then                    User.Delete();            until User.Next() = 0;    end;}