View Javadoc
1   /*
2    * SPDX-License-Identifier: MIT
3    * See LICENSE file for details.
4    *
5    * Copyright 2010-2026 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
6    */
7   package waffle.windows.auth.impl;
8   
9   import com.sun.jna.platform.win32.Advapi32Util;
10  import com.sun.jna.platform.win32.Advapi32Util.Account;
11  import com.sun.jna.platform.win32.Secur32.EXTENDED_NAME_FORMAT;
12  import com.sun.jna.platform.win32.Secur32Util;
13  
14  import waffle.windows.auth.IWindowsAccount;
15  
16  /**
17   * Windows Account.
18   */
19  public class WindowsAccountImpl implements IWindowsAccount {
20  
21      /** The account. */
22      private final Account account;
23  
24      /**
25       * Windows Account.
26       *
27       * @param newAccount
28       *            Account.
29       */
30      public WindowsAccountImpl(final Account newAccount) {
31          this.account = newAccount;
32      }
33  
34      /**
35       * Windows Account.
36       *
37       * @param userName
38       *            Fully qualified username.
39       */
40      public WindowsAccountImpl(final String userName) {
41          this(userName, null);
42      }
43  
44      /**
45       * Windows Account.
46       *
47       * @param accountName
48       *            Username, without a domain or machine.
49       * @param systemName
50       *            Machine name.
51       */
52      public WindowsAccountImpl(final String accountName, final String systemName) {
53          this(Advapi32Util.getAccountByName(systemName, accountName));
54      }
55  
56      /**
57       * Get the SAM-compatible username of the currently logged-on user.
58       *
59       * @return String.
60       */
61      public static String getCurrentUsername() {
62          return Secur32Util.getUserNameEx(EXTENDED_NAME_FORMAT.NameSamCompatible);
63      }
64  
65      /**
66       * Account domain.
67       *
68       * @return String.
69       */
70      @Override
71      public String getDomain() {
72          return this.account.domain;
73      }
74  
75      @Override
76      public String getFqn() {
77          return this.account.fqn;
78      }
79  
80      /**
81       * Account name.
82       *
83       * @return String.
84       */
85      @Override
86      public String getName() {
87          return this.account.name;
88      }
89  
90      @Override
91      public String getSidString() {
92          return this.account.sidString;
93      }
94  
95  }