From df2c0357ae19f7f8d4ee19c616cdbe409378ada5 Mon Sep 17 00:00:00 2001 From: Jens Pelzetter Date: Fri, 15 Jan 2021 09:48:54 +0100 Subject: [PATCH] Named bean for providing information about the current user Former-commit-id: df5aef34a0ded0d0c81b6d2fa278241f5fb6d707 --- .../main/java/org/libreccm/ui/UserInfo.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 ccm-core/src/main/java/org/libreccm/ui/UserInfo.java diff --git a/ccm-core/src/main/java/org/libreccm/ui/UserInfo.java b/ccm-core/src/main/java/org/libreccm/ui/UserInfo.java new file mode 100644 index 000000000..0b85e7e56 --- /dev/null +++ b/ccm-core/src/main/java/org/libreccm/ui/UserInfo.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2021 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.libreccm.ui; + +import org.libreccm.core.CoreConstants; +import org.libreccm.security.PermissionChecker; +import org.libreccm.security.Shiro; +import org.libreccm.security.User; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.inject.Named; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +@Named("UserInfo") +public class UserInfo { + + @Inject + private PermissionChecker permissionChecker; + + @Inject + private Shiro shiro; + + public String getCurrentUserName() { + return shiro + .getUser() + .map(User::getName) + .orElse(""); + } + + public boolean isAdmin() { + return permissionChecker.isPermitted(CoreConstants.PRIVILEGE_ADMIN); + } + +}