s4-tests: register new unit tests
[Samba/ekacnet.git] / source4 / scripting / python / samba / netcmd / newuser.py
blob651313a345dc98df6145d29e736de76098ffc76a
1 #!/usr/bin/python
3 # Adds a new user to a Samba4 server
4 # Copyright Jelmer Vernooij 2008
6 # Based on the original in EJS:
7 # Copyright Andrew Tridgell 2005
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 import samba.getopt as options
23 from samba.netcmd import Command, CommandError, Option
25 from getpass import getpass
26 from samba.auth import system_session
27 from samba.samdb import SamDB
29 class cmd_newuser(Command):
30 """Create a new user."""
32 synopsis = "newuser [options] <username> [<password>]"
34 takes_optiongroups = {
35 "sambaopts": options.SambaOptions,
36 "versionopts": options.VersionOptions,
37 "credopts": options.CredentialsOptions,
40 takes_options = [
41 Option("-H", help="LDB URL for database or target server", type=str),
42 Option("--unixname", help="Unix Username", type=str),
43 Option("--must-change-at-next-login",
44 help="Force password to be changed on next login",
45 action="store_true"),
48 takes_args = ["username", "password?"]
50 def run(self, username, password=None, credopts=None, sambaopts=None,
51 versionopts=None, H=None, unixname=None,
52 must_change_at_next_login=None):
53 if password is None:
54 password = getpass("New Password: ")
56 if unixname is None:
57 unixname = username
59 lp = sambaopts.get_loadparm()
60 creds = credopts.get_credentials(lp)
62 if H is not None:
63 url = H
64 else:
65 url = lp.get("sam database")
67 samdb = SamDB(url=url, session_info=system_session(), credentials=creds,
68 lp=lp)
69 samdb.newuser(username, unixname, password,
70 force_password_change_at_next_login_req=must_change_at_next_login)