Package caldavclientlibrary :: Package admin :: Package xmlaccounts :: Package commands :: Module changepassword
[hide private]
[frames] | no frames]

Source Code for Module caldavclientlibrary.admin.xmlaccounts.commands.changepassword

  1  ## 
  2  # Copyright (c) 2007-2016 Apple Inc. All rights reserved. 
  3  # 
  4  # Licensed under the Apache License, Version 2.0 (the "License"); 
  5  # you may not use this file except in compliance with the License. 
  6  # You may obtain a copy of the License at 
  7  # 
  8  # http://www.apache.org/licenses/LICENSE-2.0 
  9  # 
 10  # Unless required by applicable law or agreed to in writing, software 
 11  # distributed under the License is distributed on an "AS IS" BASIS, 
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 13  # See the License for the specific language governing permissions and 
 14  # limitations under the License. 
 15  ## 
 16   
 17  from caldavclientlibrary.admin.xmlaccounts.commands.command import Command 
 18  import getopt 
 19   
20 -class ChangePassword(Command):
21 """ 22 Command to change the password of an existing directory record. 23 """ 24 25 CMDNAME = "passwd" 26
27 - def __init__(self):
28 super(ChangePassword, self).__init__(self.CMDNAME, "Change the password for a record.") 29 self.uid = None
30
31 - def usage(self):
32 print """USAGE: %s TYPE [OPTIONS] 33 34 TYPE: One of "users", "groups", "locations" or "resources". Also, 35 "u", "g", "l" or "r" as shortcuts. 36 37 Options: 38 -f file path to accounts.xml 39 --uid UID of record to change 40 """ % (self.cmdname,)
41
42 - def execute(self, argv):
43 """ 44 Execute the command specified by the command line arguments. 45 46 @param argv: command line arguments. 47 @type argv: C{list} 48 49 @return: 1 for success, 0 for failure. 50 @rtype: C{int} 51 """ 52 53 # Check first argument for type 54 argv = self.getTypeArgument(argv) 55 if argv is None: 56 return 0 57 58 opts, args = getopt.getopt(argv, 'f:h', ["help", "uid=",]) 59 60 for name, value in opts: 61 if name == "-f": 62 self.path = value 63 elif name in ("-h", "--help"): 64 self.usage() 65 return 1 66 elif name == "--uid": 67 self.uid = value 68 else: 69 print "Unknown option: %s." % (name,) 70 self.usage() 71 return 0 72 73 if not self.path: 74 print "Must specify a path." 75 self.usage() 76 return 0 77 if not self.uid: 78 print "Must specify a UID." 79 self.usage() 80 return 0 81 if args: 82 print "Arguments not allowed." 83 self.usage() 84 return 0 85 86 if not self.loadAccounts(): 87 return 0 88 return self.doCommand()
89
90 - def doCommand(self):
91 """ 92 Run the command. 93 """ 94 if self.doChangePassword(): 95 return self.writeAccounts() 96 return 0
97
98 - def doChangePassword(self):
99 """ 100 Prompts the user for details and then changes the password of a record in the directory. 101 """ 102 103 # First check record exists 104 record = self.directory.getRecord(self.recordType, self.uid) 105 if record is None: 106 print "No '%s' record matching uid '%s'" % (self.recordType, self.uid,) 107 return 0 108 109 record.password = self.promptPassword() 110 return 1
111