From f18e3bdf2f49f3382bb9d1e39d6078ad7e1726ff Mon Sep 17 00:00:00 2001 From: ciukstar Date: Sun, 19 Nov 2023 03:18:45 +0300 Subject: [PATCH] Display roles on user profile page --- config/routes.yesodroutes | 6 +- src/Foundation.hs | 20 +++--- src/Handler/Account.hs | 41 +++++++++--- templates/about/about.hamlet | 2 +- templates/admin/business/about/about.hamlet | 2 +- templates/admin/business/brand/brand.hamlet | 2 +- templates/admin/business/business.hamlet | 2 +- .../admin/business/contact/contacts.hamlet | 2 +- .../schedule/calendar/calendar.hamlet | 2 +- .../business/schedule/hours/hours.hamlet | 2 +- templates/admin/contacts/contacts.hamlet | 2 +- templates/admin/services/services.hamlet | 2 +- templates/admin/staff/staff.hamlet | 2 +- templates/admin/users/users.hamlet | 2 +- templates/appointments/appointments.hamlet | 2 +- templates/appointments/calendar/page.hamlet | 2 +- templates/appointments/login.hamlet | 2 +- templates/auth/403.hamlet | 2 +- templates/auth/403admin.hamlet | 2 +- templates/auth/403analyst.hamlet | 2 +- templates/auth/403empl.hamlet | 2 +- templates/book/customer/banner.hamlet | 2 +- templates/book/customer/customer.hamlet | 2 +- templates/book/end.hamlet | 2 +- templates/book/offers/banner.hamlet | 2 +- templates/book/offers/offers.hamlet | 2 +- templates/book/staff/banner.hamlet | 2 +- templates/book/staff/staff.hamlet | 2 +- templates/book/time/banner.hamlet | 2 +- templates/book/time/time.hamlet | 2 +- templates/contacts/contact.hamlet | 2 +- templates/homepage.hamlet | 2 +- templates/profile.cassius | 12 +++- templates/profile.hamlet | 62 +++++++++++++++---- templates/requests/calendar/page.hamlet | 2 +- templates/requests/requests.hamlet | 2 +- templates/resources/docs.hamlet | 2 +- templates/services/services.hamlet | 2 +- templates/stats/customer/aov/aov.hamlet | 2 +- templates/stats/offers/pop.hamlet | 2 +- templates/stats/workloads/workloads.hamlet | 2 +- 41 files changed, 137 insertions(+), 76 deletions(-) diff --git a/config/routes.yesodroutes b/config/routes.yesodroutes index e423d28..1f225c1 100644 --- a/config/routes.yesodroutes +++ b/config/routes.yesodroutes @@ -50,9 +50,9 @@ /service/+Services ServiceR GET POST /services ServicesR GET -/profile ProfileR GET -/account/#UserId/photo AccountPhotoR GET -/account AccountR GET POST +/account/#UserId/profile ProfileR GET +/account/#UserId/photo AccountPhotoR GET +/account AccountR GET POST / HomeR GET diff --git a/src/Foundation.hs b/src/Foundation.hs index 8e7ec62..d82b132 100644 --- a/src/Foundation.hs +++ b/src/Foundation.hs @@ -121,19 +121,16 @@ instance Yesod App where authRoute :: App -> Maybe (Route App) authRoute _ = Just $ AuthR LoginR - isAuthorized - :: Route App -- ^ The route the user is visiting. - -> Bool -- ^ Whether or not this is a "write" request. - -> Handler AuthResult - -- Routes not requiring authentication. + isAuthorized :: Route App -> Bool -> Handler AuthResult + + isAuthorized (StaticR _) _ = return Authorized + isAuthorized (AuthR _) _ = return Authorized isAuthorized HomeR _ = return Authorized isAuthorized FaviconR _ = return Authorized isAuthorized RobotsR _ = return Authorized isAuthorized PhotoPlaceholderR _ = return Authorized - isAuthorized (StaticR _) _ = return Authorized - isAuthorized r@(StatsR PopOffersR) _ = setUltDest r >> isAnalyst isAuthorized r@(StatsR WorkloadsR) _ = setUltDest r >> isAnalyst isAuthorized r@(StatsR (WorkloadEmplMonthR _ _)) _ = setUltDest r >> isAnalyst @@ -225,8 +222,6 @@ instance Yesod App where isAuthorized r@(AdminR (BusinessContactEditR _ _)) _ = setUltDest r >> isAdmin isAuthorized r@(AdminR (BusinessContactDeleteR _ _)) _ = setUltDest r >> isAdmin - - isAuthorized ContactR _ = return Authorized isAuthorized BookEndR _ = return Authorized @@ -260,12 +255,11 @@ instance Yesod App where isAuthorized r@(TasksDayListR {}) _ = setUltDest r >> isEmployee isAuthorized r@(TaskItemR {}) _ = setUltDest r >> isEmployee isAuthorized r@(TaskHistR {}) _ = setUltDest r >> isEmployee - - - + + isAuthorized (ProfileR _) _ = isAuthenticated + isAuthorized AccountR _ = return Authorized isAuthorized (AccountPhotoR _) _ = return Authorized - isAuthorized ProfileR _ = isAuthenticated isAuthorized ServicesR _ = return Authorized isAuthorized (ServiceR _) _ = return Authorized diff --git a/src/Handler/Account.hs b/src/Handler/Account.hs index a1009b6..bfce1b5 100644 --- a/src/Handler/Account.hs +++ b/src/Handler/Account.hs @@ -41,13 +41,18 @@ import Yesod.Auth (Route (LoginR, LogoutR), maybeAuth) import Foundation ( Handler, Widget - , Route (StaticR, AccountPhotoR, HomeR, AccountR, PhotoPlaceholderR, AuthR) + , Route + ( StaticR, AccountPhotoR, HomeR, AccountR, PhotoPlaceholderR + , AuthR, AdminR + ) + , AdminR (AdmStaffPhotoR) , AppMessage ( MsgAccount, MsgCancel, MsgUsername, MsgPassword , MsgPhoto, MsgFullName, MsgEmail, MsgSignUp, MsgBack - , MsgConfirmPassword, MsgYouMustEnterTwoValues + , MsgConfirmPassword, MsgYouMustEnterTwoValues, MsgEmployee , MsgPasswordsDoNotMatch, MsgRegistration, MsgUserProfile - , MsgLogout, MsgLogin, MsgLoginToSeeYourProfile + , MsgLogout, MsgLogin, MsgLoginToSeeYourProfile, MsgRoles + , MsgAdministrator, MsgAnalyst ) ) @@ -57,22 +62,38 @@ import Database.Persist (Entity (Entity), insert, insert_) import Model ( ultDestKey - , User (userName, User, userPassword, userFullName, userEmail), UserId + , UserId, User (userName, User, userPassword, userFullName, userEmail) , UserPhoto (UserPhoto, userPhotoUser, userPhotoPhoto, userPhotoMime) - , EntityField (UserPhotoUser) + , EntityField (UserPhotoUser, StaffUser, RoleStaff, RoleName) + , Staff (Staff), Role ) import Database.Esqueleto.Experimental - (selectOne, from, table, where_ - , (^.), (==.), val + (Value (Value), selectOne, from, table, where_ + , (^.), (==.), val, just, select, distinct ) -import Settings.StaticFiles (img_add_photo_alternate_FILL0_wght400_GRAD0_opsz48_svg) +import Settings.StaticFiles + ( img_add_photo_alternate_FILL0_wght400_GRAD0_opsz48_svg ) + -getProfileR :: Handler Html -getProfileR = do +getProfileR :: UserId -> Handler Html +getProfileR uid = do user <- maybeAuth ult <- getUrlRender >>= \rndr -> fromMaybe (rndr HomeR) <$> lookupSession ultDestKey + + empl <- runDB $ selectOne $ do + x <- from $ table @Staff + where_ $ x ^. StaffUser ==. just (val uid) + return x + + jobs <- case empl of + Just (Entity eid _) -> runDB $ select $ distinct $ do + x <- from $ table @Role + where_ $ x ^. RoleStaff ==. val eid + return $ x ^. RoleName + Nothing -> return [] + defaultLayout $ do setTitleI MsgUserProfile $(widgetFile "profile") diff --git a/templates/about/about.hamlet b/templates/about/about.hamlet index 931202f..67ace4a 100644 --- a/templates/about/about.hamlet +++ b/templates/about/about.hamlet @@ -11,7 +11,7 @@ $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - + _{MsgPhoto}search $if null sids $maybe Entity uid _ <- user - + _{MsgPhoto} search $maybe Entity uid _ <- user - + _{MsgPhoto} search $maybe Entity uid _ <- user - + _{MsgPhoto} search $maybe Entity uid _ <- user - + _{MsgPhoto} search $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - + _{MsgPhoto} diff --git a/templates/auth/403.hamlet b/templates/auth/403.hamlet index b071ac9..7d5761a 100644 --- a/templates/auth/403.hamlet +++ b/templates/auth/403.hamlet @@ -9,7 +9,7 @@ $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - + _{MsgPhoto} search $maybe Entity uid _ <- user - + _{MsgPhoto} search $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - + _{MsgPhoto} - _{MsgPhoto} + _{MsgPhoto}
- #{uname} - $maybe fname <- fname -

- #{fname} - $maybe email <- email + $maybe fname <- fname + #{fname}

+ #{uname} + $nothing + #{uname} + $maybe email <- email +
+ #{email} - $nothing - $maybe email <- email -

- #{email} - _{MsgLogout} + _{MsgLogout} $nothing

@@ -41,3 +40,42 @@ _{MsgLogin} + + $maybe Entity _ (User _ _ admin analyst _ _) <- user + $if admin || analyst + + + + + info + + + _{MsgRoles} + + expand_more +
    + $forall (_,role) <- filter fst [(admin,MsgAdministrator),(analyst,MsgAnalyst)] +
  • _{role} + + $maybe Entity eid (Staff ename _ _ _ _ _) <- empl + + + + + info + + + _{MsgEmployee} + + expand_more +
    + _{MsgPhoto} +
    + #{ename} + + $if not (null jobs) +
      + $forall Value job <- jobs +
    • #{job} diff --git a/templates/requests/calendar/page.hamlet b/templates/requests/calendar/page.hamlet index 6e95e50..3a19d25 100644 --- a/templates/requests/calendar/page.hamlet +++ b/templates/requests/calendar/page.hamlet @@ -18,7 +18,7 @@ search $maybe Entity uid _ <- user - + _{MsgPhoto} search $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - + _{MsgPhoto} search $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - + _{MsgPhoto} $maybe Entity uid _ <- user - +