Add admin object

This commit is contained in:
2025-10-08 00:26:40 +02:00
parent 8db241d45f
commit 601699f7ad
7 changed files with 138 additions and 8 deletions

View File

@@ -7,14 +7,28 @@ Class {
{ #category : 'rendering' } { #category : 'rendering' }
TBAdminHeaderComponent >> renderButtonsOn: html [ TBAdminHeaderComponent >> renderButtonsOn: html [
html form: [ self renderDisconnectButtonOn: html ] html form: [
self renderDisconnectButtonOn: html.
self renderPublicViewButton: html ]
] ]
{ #category : 'rendering' } { #category : 'rendering' }
TBAdminHeaderComponent >> renderDisconnectButtonOn: html [ TBAdminHeaderComponent >> renderDisconnectButtonOn: html [
html formButton beSecondary; html formButton
callback: [ component goToPostListView ]; beSecondary;
callback: [ self session reset ];
with: [ with: [
html text: 'Disconnect'. html text: 'Disconnect'.
html span class: 'glyphicon glyphicon-logout' ] html span class: 'glyphicon glyphicon-logout' ]
] ]
{ #category : 'rendering' }
TBAdminHeaderComponent >> renderPublicViewButton: html [
self session isLogged ifTrue: [
html formButton
beSecondary;
callback: [ component goToPostListView ];
with: [
html span class: 'glyphicon glyphicon-eye-open'.
html text: ' Public View' ] ]
]

View File

@@ -18,6 +18,8 @@ TBApplicationRootComponent class >> initialize [
"self initialize" "self initialize"
| app | | app |
app := WAAdmin register: self asApplicationAt: 'TinyBlog'. app := WAAdmin register: self asApplicationAt: 'TinyBlog'.
app
preferenceAt: #sessionClass put: TBSession.
app app
addLibrary: JQDeploymentLibrary; addLibrary: JQDeploymentLibrary;
addLibrary: JQUiDeploymentLibrary; addLibrary: JQUiDeploymentLibrary;

View File

@@ -36,7 +36,9 @@ TBHeaderComponent >> renderBrandOn: html [
{ #category : 'rendering' } { #category : 'rendering' }
TBHeaderComponent >> renderButtonsOn: html [ TBHeaderComponent >> renderButtonsOn: html [
self renderModalLoginButtonOn: html self session isLogged
ifTrue: [ self renderSimpleAdminButtonOn: html ]
ifFalse: [ self renderModalLoginButtonOn: html ]
] ]
{ #category : 'rendering' } { #category : 'rendering' }
@@ -63,3 +65,14 @@ TBHeaderComponent >> renderModalLoginButtonOn: html [
html span class: 'glyphicon glyphicon-lock'. html span class: 'glyphicon glyphicon-lock'.
html text: 'Login' ] html text: 'Login' ]
] ]
{ #category : 'rendering' }
TBHeaderComponent >> renderSimpleAdminButtonOn: html [
html form: [
html formButton
beSecondary;
callback: [ component goToAdministrationView ];
with: [
html span class: 'glyphicon glyphicon-list-alt'.
html text: ' Admin View' ]]
]

View File

@@ -134,7 +134,9 @@ TBPostsListComponent >> renderPostColumnOn: html [
{ #category : 'as yet unclassified' } { #category : 'as yet unclassified' }
TBPostsListComponent >> tryConnectionWithLogin: login andPassword: password [ TBPostsListComponent >> tryConnectionWithLogin: login andPassword: password [
(login = 'admin' and: [ password = 'topsecret' ]) (login = self blog administrator login and: [ (SHA256 hashMessage: password) = self blog administrator password ])
ifTrue: [ self goToAdministrationView ] ifTrue: [
self session currentAdmin: self blog administrator.
self goToAdministrationView ]
ifFalse: [ self loginErrorOccurred ] ifFalse: [ self loginErrorOccurred ]
] ]

View File

@@ -0,0 +1,33 @@
Class {
#name : 'TBSession',
#superclass : 'WASession',
#instVars : [
'currentAdmin'
],
#category : 'TinyBlog-Components',
#package : 'TinyBlog-Components'
}
{ #category : 'accessing' }
TBSession >> currentAdmin [
^ currentAdmin
]
{ #category : 'accessing' }
TBSession >> currentAdmin: anObject [
currentAdmin := anObject
]
{ #category : 'testing' }
TBSession >> isLogged [
^ self currentAdmin notNil
]
{ #category : 'initialization' }
TBSession >> reset [
currentAdmin := nil.
self requestContext redirectTo: self application url.
self unregister
]

View File

@@ -0,0 +1,42 @@
Class {
#name : 'TBAdministrator',
#superclass : 'Object',
#instVars : [
'login',
'password'
],
#category : 'TinyBlog',
#package : 'TinyBlog'
}
{ #category : 'as yet unclassified' }
TBAdministrator class >> login: login password: password [
^ self new
login: login;
password: password;
yourself
]
{ #category : 'accessing' }
TBAdministrator >> login [
^ login
]
{ #category : 'accessing' }
TBAdministrator >> login: anObject [
login := anObject
]
{ #category : 'accessing' }
TBAdministrator >> password [
^ password
]
{ #category : 'accessing' }
TBAdministrator >> password: anObject [
password := SHA256 hashMessage: anObject
]

View File

@@ -2,7 +2,8 @@ Class {
#name : 'TBBlog', #name : 'TBBlog',
#superclass : 'Object', #superclass : 'Object',
#instVars : [ #instVars : [
'posts' 'posts',
'adminUser'
], ],
#category : 'TinyBlog', #category : 'TinyBlog',
#package : 'TinyBlog' #package : 'TinyBlog'
@@ -38,6 +39,16 @@ TBBlog class >> current [
ifEmpty: [ self new save ] ifEmpty: [ self new save ]
] ]
{ #category : 'as yet unclassified' }
TBBlog class >> defaultAdminLogin [
^ 'admin'
]
{ #category : 'as yet unclassified' }
TBBlog class >> defaultAdminPassword [
^ 'topsecret'
]
{ #category : 'initialization' } { #category : 'initialization' }
TBBlog class >> initialize [ TBBlog class >> initialize [
self reset self reset
@@ -59,6 +70,11 @@ TBBlog class >> reset [
self initializeVoyageOnMemoryDB. self initializeVoyageOnMemoryDB.
] ]
{ #category : 'as yet unclassified' }
TBBlog >> administrator [
^ adminUser
]
{ #category : 'reading' } { #category : 'reading' }
TBBlog >> allBlogPosts [ TBBlog >> allBlogPosts [
^ posts ^ posts
@@ -84,10 +100,18 @@ TBBlog >> allVisibleBlogPostsFromCategory: aCategory [
^ posts select: [ :p | p category = aCategory and: [ p isVisible ] ] ^ posts select: [ :p | p category = aCategory and: [ p isVisible ] ]
] ]
{ #category : 'as yet unclassified' }
TBBlog >> createAdministrator [
^ TBAdministrator
login: self class defaultAdminLogin
password: self class defaultAdminPassword
]
{ #category : 'initialization' } { #category : 'initialization' }
TBBlog >> initialize [ TBBlog >> initialize [
super initialize. super initialize.
posts := OrderedCollection new posts := OrderedCollection new.
adminUser := self createAdministrator
] ]
{ #category : 'deleting' } { #category : 'deleting' }