summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Young <ayoung@redhat.com>2010-07-20 18:20:34 -0400
committerAdam Young <ayoung@redhat.com>2010-07-20 18:20:34 -0400
commit90fefd437a4e338588c33e47efc19563fa7b9cd3 (patch)
treee56c3ec031734a93b42a04d62b1229a812e7d064
parentcb4e49ef4cf21c78c148bc5f187e986fe95afed9 (diff)
Added pluging to reflect user info, needed for web ui.
-rw-r--r--ipalib/plugins/whoami.py105
1 files changed, 105 insertions, 0 deletions
diff --git a/ipalib/plugins/whoami.py b/ipalib/plugins/whoami.py
new file mode 100644
index 000000000..3c4d9df7d
--- /dev/null
+++ b/ipalib/plugins/whoami.py
@@ -0,0 +1,105 @@
+# Authors:
+# Pavel Zuna <pzuna@redhat.com>
+#
+# Copyright (C) 2010 Red Hat
+# see file 'COPYING' for use and warranty information
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; version 2 only
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+"""
+Example plugins
+"""
+
+# Hey guys, so you're interested in writing plugins for IPA? Great!
+# We compiled this small file with examples on how to extend IPA to suit
+# your needs. We'll be going from very simple to pretty complex plugins
+# hopefully covering most of what our framework has to offer.
+
+# First, let's import some stuff.
+
+# api is an object containing references to all plugins and useful classes.
+# errors is a module containing all IPA specific exceptions.
+from ipalib import api, errors
+# Command is the base class for command plugin.
+from ipalib import Command
+# Str is a subclass of Param, it is used to define string parameters for
+# command. We'll go through all other subclasses of Param supported by IPA
+# later in this file
+from ipalib import Str
+# output is a module containing the most common output patterns.
+# Command plugin do output validation based on these patterns.
+# You can define your own as we're going to show you later.
+from ipalib import output, util
+
+
+# We're going to create an example command plugin, that takes a name as its
+# only argument. Commands in IPA support input validation by defining
+# functions we're going to call 'validators'. This is an example of such
+# function:
+def validate_name(ugettext, name):
+ """
+ Validate names for the whoami command. Names starting with 'Y'
+ (picked at random) are considered invalid.
+ """
+ if name.startswith('Y'):
+ raise errors.ValidationError(
+ name='name',
+ error='Names starting with \'Y\' are invalid!'
+ )
+ # If the validator doesn't return anything (i.e. it returns None),
+ # the parameter passes validation.
+
+
+class whoami(Command):
+ """
+ Example commnad: Hello world!
+ """
+ # takes_args is an attribute of Command. It's a tuple containing
+ # instances of Param (or its subclasses such as Str) that define
+ # what position arguments are accepted by the command.
+ takes_args = (
+ # The first argument of Param constructor is the name that will be
+ # used to identify this parameter. It can be followed by validator
+ # functions. The constructor can also take a bunch of keyword
+ # arguments. Here we use default, to set the parameters default value
+ # and autofill, that fills the default value if the parameter isn't
+ # present.
+ # Note the ? at the end of the parameter name. It makes the parameter
+ # optional.
+ Str('name?', validate_name,
+ default=u'anonymous coward',
+ autofill=True,
+ ),
+ )
+
+ # has_output is an attribute of Command, it is a tuple containing
+ # output.Output instances that define its output pattern.
+ # Commands in IPA return dicts with keys corresponding to items
+ # in the has_output tuple.
+ has_output = (
+ # output.summary is one of the basic patterns.
+ # It's a string that should be filled with a user-friendly
+ # decription of the action performed by the command.
+ output.summary,
+ )
+
+ # Every command needs to override the execute method.
+ # This is where the command functionality should go.
+ # It is always executed on the server-side, so don't rely
+ # on client-side stuff in here!
+ def execute(self, name):
+ return dict(summary=util.get_current_principal())
+
+# register the command, uncomment this line if you want to try it out
+api.register(whoami)
+