From fa86542f90774d7a7d8f3e81ee35821ca25b7fd4 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Fri, 18 Mar 2016 23:06:32 -0400 Subject: Added pluggable Avatar providers --- ChangeLog | 1 + app/Core/Base.php | 1 + app/Core/User/Avatar/AvatarManager.php | 62 ++++++++++++++++++++++++ app/Core/User/Avatar/AvatarProviderInterface.php | 30 ++++++++++++ app/Helper/UserHelper.php | 17 +++---- app/ServiceProvider/AvatarProvider.php | 31 ++++++++++++ app/Template/comment/show.php | 4 +- app/Template/event/comment_create.php | 2 - app/Template/event/comment_update.php | 2 - app/Template/event/events.php | 11 ++++- app/Template/event/subtask_create.php | 2 - app/Template/event/subtask_update.php | 2 - app/Template/event/task_assignee_change.php | 2 - app/Template/event/task_close.php | 2 - app/Template/event/task_create.php | 2 - app/Template/event/task_file_create.php | 2 - app/Template/event/task_move_column.php | 2 - app/Template/event/task_move_position.php | 2 - app/Template/event/task_move_swimlane.php | 2 - app/Template/event/task_open.php | 2 - app/Template/event/task_update.php | 2 - app/User/Avatar/GravatarProvider.php | 35 +++++++++++++ app/common.php | 1 + doc/plugin-avatar-provider.markdown | 32 ++++++++++++ doc/plugins.markdown | 7 +-- 25 files changed, 216 insertions(+), 42 deletions(-) create mode 100644 app/Core/User/Avatar/AvatarManager.php create mode 100644 app/Core/User/Avatar/AvatarProviderInterface.php create mode 100644 app/ServiceProvider/AvatarProvider.php create mode 100644 app/User/Avatar/GravatarProvider.php create mode 100644 doc/plugin-avatar-provider.markdown diff --git a/ChangeLog b/ChangeLog index 79c389a9..ef24f486 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,7 @@ Version 1.0.27 (unreleased) Improvements: +* Added pluggable Avatar providers * Improve task summary sections * Put back the action sidebar in task view * Added support for multiple placeholders for LDAP_USER_FILTER diff --git a/app/Core/Base.php b/app/Core/Base.php index e53f299a..f87f271a 100644 --- a/app/Core/Base.php +++ b/app/Core/Base.php @@ -41,6 +41,7 @@ use Pimple\Container; * @property \Kanboard\Core\Session\FlashMessage $flash * @property \Kanboard\Core\Session\SessionManager $sessionManager * @property \Kanboard\Core\Session\SessionStorage $sessionStorage + * @property \Kanboard\Core\User\Avatar\AvatarManager $avatarManager * @property \Kanboard\Core\User\GroupSync $groupSync * @property \Kanboard\Core\User\UserProfile $userProfile * @property \Kanboard\Core\User\UserSync $userSync diff --git a/app/Core/User/Avatar/AvatarManager.php b/app/Core/User/Avatar/AvatarManager.php new file mode 100644 index 00000000..4b0c5298 --- /dev/null +++ b/app/Core/User/Avatar/AvatarManager.php @@ -0,0 +1,62 @@ +providers[] = $provider; + return $this; + } + + /** + * Render avatar html element + * + * @access public + * @param string $user_id + * @param string $username + * @param string $name + * @param string $email + * @param int $size + * @return string + */ + public function render($user_id, $username, $name, $email, $size) + { + $user = array( + 'id' => $user_id, + 'username' => $username, + 'name' => $name, + 'email' => $email, + ); + + foreach ($this->providers as $provider) { + if ($provider->isActive($user)) { + return $provider->render($user, $size); + } + } + + return ''; + } +} diff --git a/app/Core/User/Avatar/AvatarProviderInterface.php b/app/Core/User/Avatar/AvatarProviderInterface.php new file mode 100644 index 00000000..e0375d26 --- /dev/null +++ b/app/Core/User/Avatar/AvatarProviderInterface.php @@ -0,0 +1,30 @@ +config->get('integration_gravatar') == 1) { - return ''.$this->helper->text->e($alt).''; - } - - return ''; + $html = $this->avatarManager->render($user_id, $username, $name, $email, 25); + return '
'.$html.'
'; } } diff --git a/app/ServiceProvider/AvatarProvider.php b/app/ServiceProvider/AvatarProvider.php new file mode 100644 index 00000000..2bbc9c03 --- /dev/null +++ b/app/ServiceProvider/AvatarProvider.php @@ -0,0 +1,31 @@ +register(new GravatarProvider($container)); + return $container; + } +} diff --git a/app/Template/comment/show.php b/app/Template/comment/show.php index 873e1470..c5963666 100644 --- a/app/Template/comment/show.php +++ b/app/Template/comment/show.php @@ -1,9 +1,7 @@

- - user->avatar($comment['email'], $comment['name'] ?: $comment['username']) ?> - + user->avatar($comment['user_id'], $comment['username'], $comment['name'], $comment['email']) ?> text->e($comment['name'] ?: $comment['username']) ?> @ diff --git a/app/Template/event/comment_create.php b/app/Template/event/comment_create.php index 063736b3..43c521cf 100644 --- a/app/Template/event/comment_create.php +++ b/app/Template/event/comment_create.php @@ -1,5 +1,3 @@ -user->avatar($email, $author) ?> -

text->e($author), diff --git a/app/Template/event/comment_update.php b/app/Template/event/comment_update.php index 93f24d8a..80a33031 100644 --- a/app/Template/event/comment_update.php +++ b/app/Template/event/comment_update.php @@ -1,5 +1,3 @@ -user->avatar($email, $author) ?> -

text->e($author), diff --git a/app/Template/event/events.php b/app/Template/event/events.php index bbb01be4..097da318 100644 --- a/app/Template/event/events.php +++ b/app/Template/event/events.php @@ -16,7 +16,16 @@  dt->datetime($event['date_creation']) ?>

-
+
+ user->avatar( + $event['creator_id'], + $event['author_username'], + $event['author_name'], + $event['email'] + ) ?> + + +
diff --git a/app/Template/event/subtask_create.php b/app/Template/event/subtask_create.php index 532783d5..ae6ddd44 100644 --- a/app/Template/event/subtask_create.php +++ b/app/Template/event/subtask_create.php @@ -1,5 +1,3 @@ -user->avatar($email, $author) ?> -

text->e($author), diff --git a/app/Template/event/subtask_update.php b/app/Template/event/subtask_update.php index e9ff6ac6..1d560fb0 100644 --- a/app/Template/event/subtask_update.php +++ b/app/Template/event/subtask_update.php @@ -1,5 +1,3 @@ -user->avatar($email, $author) ?> -

text->e($author), diff --git a/app/Template/event/task_assignee_change.php b/app/Template/event/task_assignee_change.php index 580176c7..6964a50e 100644 --- a/app/Template/event/task_assignee_change.php +++ b/app/Template/event/task_assignee_change.php @@ -1,5 +1,3 @@ -user->avatar($email, $author) ?> -

diff --git a/app/Template/event/task_close.php b/app/Template/event/task_close.php index 361458d3..2ba77156 100644 --- a/app/Template/event/task_close.php +++ b/app/Template/event/task_close.php @@ -1,5 +1,3 @@ -user->avatar($email, $author) ?> -

text->e($author), diff --git a/app/Template/event/task_create.php b/app/Template/event/task_create.php index 655bf8f3..21facc9e 100644 --- a/app/Template/event/task_create.php +++ b/app/Template/event/task_create.php @@ -1,5 +1,3 @@ -user->avatar($email, $author) ?> -

text->e($author), diff --git a/app/Template/event/task_file_create.php b/app/Template/event/task_file_create.php index 61bf3d61..837f5dc0 100644 --- a/app/Template/event/task_file_create.php +++ b/app/Template/event/task_file_create.php @@ -1,5 +1,3 @@ -user->avatar($email, $author) ?> -

text->e($author), diff --git a/app/Template/event/task_move_column.php b/app/Template/event/task_move_column.php index 904c956c..b9ae65cc 100644 --- a/app/Template/event/task_move_column.php +++ b/app/Template/event/task_move_column.php @@ -1,5 +1,3 @@ -user->avatar($email, $author) ?> -

text->e($author), diff --git a/app/Template/event/task_move_position.php b/app/Template/event/task_move_position.php index 6580bb79..dbe73f4b 100644 --- a/app/Template/event/task_move_position.php +++ b/app/Template/event/task_move_position.php @@ -1,5 +1,3 @@ -user->avatar($email, $author) ?> -

text->e($author), diff --git a/app/Template/event/task_move_swimlane.php b/app/Template/event/task_move_swimlane.php index 9ffa554c..b0635a06 100644 --- a/app/Template/event/task_move_swimlane.php +++ b/app/Template/event/task_move_swimlane.php @@ -1,5 +1,3 @@ -user->avatar($email, $author) ?> -

user->avatar($email, $author) ?> -

text->e($author), diff --git a/app/Template/event/task_update.php b/app/Template/event/task_update.php index 72d70495..83f1661b 100644 --- a/app/Template/event/task_update.php +++ b/app/Template/event/task_update.php @@ -1,5 +1,3 @@ -user->avatar($email, $author) ?> -

text->e($author), diff --git a/app/User/Avatar/GravatarProvider.php b/app/User/Avatar/GravatarProvider.php new file mode 100644 index 00000000..10ab4390 --- /dev/null +++ b/app/User/Avatar/GravatarProvider.php @@ -0,0 +1,35 @@ +helper->text->e($user['name'] ?: $user['username']); + return ''.$title.''; + } + + /** + * Determine if the provider is active + * + * @access public + * @param array $user + * @return boolean + */ + public function isActive(array $user) + { + return !empty($user['email']) && $this->config->get('integration_gravatar') == 1; + } +} diff --git a/app/common.php b/app/common.php index 71f80c75..7dbd7587 100644 --- a/app/common.php +++ b/app/common.php @@ -38,4 +38,5 @@ $container->register(new Kanboard\ServiceProvider\GroupProvider); $container->register(new Kanboard\ServiceProvider\RouteProvider); $container->register(new Kanboard\ServiceProvider\ActionProvider); $container->register(new Kanboard\ServiceProvider\ExternalLinkProvider); +$container->register(new Kanboard\ServiceProvider\AvatarProvider); $container->register(new Kanboard\ServiceProvider\PluginProvider); diff --git a/doc/plugin-avatar-provider.markdown b/doc/plugin-avatar-provider.markdown new file mode 100644 index 00000000..62941e5c --- /dev/null +++ b/doc/plugin-avatar-provider.markdown @@ -0,0 +1,32 @@ +Adding a new Avatar Provider +============================= + +Registration +------------ + +```php +$this->avatarManager->register(new CustomAvatarProvider()); +``` + +Interface +--------- + +The provider must implements the interface `Kanboard\Core\User\Avatar\AvatarProviderInterface`: + + +| Method | Description | +|-------------------------------|---------------------------------------------------------------| +| `render(array $user, $size)` | Render HTML | +| `isActive(array $user)` | Returns a boolean if the provider is able to render something | + + +The `$user` argument is a dictionary that contains these keys: + +```php +[ + 'id' => 123, + 'username' => 'admin', + 'name' => 'Administrator', + 'email' => 'me@localhost', +] +``` diff --git a/doc/plugins.markdown b/doc/plugins.markdown index e38c887f..475bc249 100644 --- a/doc/plugins.markdown +++ b/doc/plugins.markdown @@ -20,9 +20,10 @@ Plugin creators should specify explicitly the compatible versions of Kanboard. I - [Attach metadata to users, tasks and projects](plugin-metadata.markdown) - [Authentication architecture](plugin-authentication-architecture.markdown) - [Authentication plugin registration](plugin-authentication.markdown) -- [Authorization Architecture](plugin-authorization-architecture.markdown) -- [Custom Group Providers](plugin-group-provider.markdown) -- [External Link Providers](plugin-external-link.markdown) +- [Authorization architecture](plugin-authorization-architecture.markdown) +- [Custom group providers](plugin-group-provider.markdown) +- [External link providers](plugin-external-link.markdown) +- [Add avatar providers](plugin-avatar-provider.markdown) - [LDAP client](plugin-ldap-client.markdown) Examples of plugins -- cgit v1.2.3