public class WordPressAccount extends jespa.security.Properties implements Account
WordPressSecurityProvider.getAccount(java.lang.String, java.lang.String[])
.
Constructor and Description |
---|
WordPressAccount(WordPressSecurityProvider provider,
java.lang.String acctname) |
Modifier and Type | Method and Description |
---|---|
void |
changePassword(char[] oldpassword,
char[] newpassword)
Changes the password for this account to the new password only if the old password is correct.
|
void |
create()
Create a new account in the WordPress database with all of the attributes of this object.
|
void |
create(java.lang.String[] attrs)
Create a new account in the WordPress database with selected attribute values of this object.
|
void |
delete()
Delete an existing account in the WordPress database.
|
java.lang.Object |
getProperty(java.lang.String name,
java.lang.Object def)
Retrieve account attribtues and properties of this account instance.
|
boolean |
isMemberOf(java.lang.String role)
Returns true if this account has the specified role.
|
void |
setPassword(char[] password)
Set the password for this account to the supplied value in the WordPress database.
|
void |
setProperty(java.lang.String name,
java.lang.Object obj)
Set account attributes and properties of this account instance.
|
void |
update()
Insert, update or delete attributes on an existing account in the WordPress database with all attribute values of this object.
|
void |
update(java.lang.String[] attrs)
Insert, update or delete attributes on an existing account in the WordPress database with selected attribute values of this object.
|
decodeObject, encodeObject, getEncryptedProperty, getFilteredProperties, getFilteredProperties, getProperty, getPropertyAsBoolean, getPropertyAsLong, setEncryptedProperty
clear, clone, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, forEach, get, getOrDefault, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
finalize, getClass, notify, notifyAll, wait, wait, wait
getProperty
public WordPressAccount(WordPressSecurityProvider provider, java.lang.String acctname) throws SecurityProviderException
SecurityProviderException
public void setProperty(java.lang.String name, java.lang.Object obj) throws SecurityProviderException
create()
or update(String[])
will actually INSERT, UPDATE and DELETE data in the WordPress database.
This method validates the user_login attribute using the regular expression defined by the WordPressSecurityProvider property account.name.regex which by default is "\S+" to indicate that the user_login must consist entirely of one or more non-space characters.
setProperty
in interface Account
setProperty
in class jespa.security.Properties
name
- The name of the property or attribute to set. If this parameter is user_login, validation is performed.obj
- The value of the property or attribute being set.SecurityProviderException
- if the user_login attribute is set and it either fails validation or contains a domain component that does not match configured domain properties.public java.lang.Object getProperty(java.lang.String name, java.lang.Object def) throws SecurityProviderException
WordPressSecurityProvider.getAccount(java.lang.String, java.lang.String[])
and setProperty(java.lang.String, java.lang.Object)
methods.
Property Name | Description |
---|---|
capabilities | A read-only constructed property that retrieves a Map representing the wp_capabilities from the wp_usermeta table for this account. By default, this Map should contain a "1" (the PHP representation of true) keyed by the name of the role the account is in. |
Consider the following code fragment that retrieves and prints elements of the capabilities property:
Map capabilities = (Map)acct.getProperty("capabilities"). if (capabilities != null) { Iterator iter = capabilities.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); } }
This getProperty method should be favored over the get method so that the implementation can intercept or construct values such as the above mentioned capabilities property.
getProperty
in interface Account
getProperty
in class jespa.security.Properties
name
- The name of the property or attribute to retrieve.def
- The default value to return if the named property or attribute is not set.SecurityProviderException
public void create(java.lang.String[] attrs) throws SecurityProviderException
See the create()
method description for additional information.
create
in interface Account
attrs
- A list of attribute names limiting the attributes to be created.SecurityProviderException
- if the user_login or user_email attributes are not present or if an SQL error occurs.public void create() throws SecurityProviderException
WordPressSecurityProvider wpsp = new WordPressSecurityProvider(props); try { WordPressAccount acct = new WordPressAccount(wpsp, acctname); acct.setProperty("user_email", email) ; acct.create(); acct.setPassword(password.toCharArray()); } finally { wpsp.dispose(); }
Some notes regarding the above example are:
Unless set otherwise or filtered out using create(String[])
, the following default attributes and values will be added:
Attribute Name | Default Value |
---|---|
user_nicename | user_login |
display_name | user_login |
nickname | user_login |
rich_editing | true |
comment_shortcuts | false |
admin_color | fresh |
wp_capabilities | a:1:{s:10:"subscriber";b:1;} |
This method is equivalent to create(Account.ALL_ATTRS)
.
create
in interface Account
SecurityProviderException
- if the user_login or user_email attributes are not present or if an SQL error occurs.public void update(java.lang.String[] attrs) throws SecurityProviderException
All attributes being updated must first be retrieved with a method like WordPressSecurityProvider.getAccount(java.lang.String, java.lang.String[])
.
The following example will update only the user_email attribute.
WordPressSecurityProvider wpsp = new WordPressSecurityProvider(props); try { String[] attrs = new String[] { "user_login", "user_email" }; WordPressAccount acct = (WordPressAccount)wpsp.getAccount(acctname, attrs); acct.setProperty("user_email", "alice.baker@example.local"); acct.update(attrs); } finally { wpsp.dispose(); }
Condition | Resulting SQL Operation |
---|---|
If the attribute is not in the original copy but is present in this object. | INSERT |
If the attribute is present in the original copy and in this object but it's value has changed. | UPDATE |
If the attribute is in the original copy but has since been removed from this object. | DELETE |
Otherwise, no action is taken (becuase the attribute is not in either the original copy or this object or it is present in both but the value has not changed). | No action |
Note that if the original copy of the account does not accurately represent exiting database values for the attributes being updated, unexpected results may occur. Specifically, if the WordPressSecurityProvider.getAccount() method was not used or did not retrieve an attribute to be updated (because it was not included in the attrs parameter), the update will attempt to INSERT the value when it should have used UPDATE. This will likely result in an SQL error. Similarly, removing an attribute will not emit the desired DELETE operation if the original copy does not contain the corresponding database value.
If present, the ID attribute of the wp_users table and the umeta_id and user_id fields of the wp_usermeta table are not used by and cannot be modified by this implementation (all operations hinge on the user_login attribute).
update
in interface Account
attrs
- A list of attribute names indicating the attributes to be updated.
If the attrs parameter is the special Account.ALL_ATTRS constant, all attributes of this account instance will be updated.SecurityProviderException
- if the user_login attribute is not present or if an SQL error occurs.public void update() throws SecurityProviderException
See the update(String[])
method description for additional information.
This method is equivalent to update(Account.ALL_ATTRS)
.
update
in interface Account
SecurityProviderException
- if the user_login attribute is not present or if an SQL error occurs.public void delete() throws SecurityProviderException
delete
in interface Account
SecurityProviderException
- if the account does not exist or if an SQL error occurs.public boolean isMemberOf(java.lang.String role) throws SecurityProviderException
This method retrieves the wp_capabilities field of the wp_usermeta table, unserializes it to a Map and verifies that it contains a key with the supplied role name mapped to a value of "1" (which is the PHP representation of true).
getProperty(java.lang.String, java.lang.Object)
for details.
isMemberOf
in interface Account
role
- the name of the group to check.SecurityProviderException
- if the account identified by this object cannot be found or if an SQL error occurs.public void setPassword(char[] password) throws SecurityProviderException
setPassword
in interface Account
SecurityProviderException
- if the account is not found or if an SQL error occurs.public void changePassword(char[] oldpassword, char[] newpassword) throws SecurityProviderException
changePassword
in interface Account
oldpassword
- the current password for this account.newpassword
- the new password to be set on this account.SecurityProviderException
- if the account is not found or if an SQLException occurs