diff --git a/src/TinyBlog-Components/TBAdminHeaderComponent.class.st b/src/TinyBlog-Components/TBAdminHeaderComponent.class.st index 6f90c70..cf7e8cd 100644 --- a/src/TinyBlog-Components/TBAdminHeaderComponent.class.st +++ b/src/TinyBlog-Components/TBAdminHeaderComponent.class.st @@ -7,14 +7,28 @@ Class { { #category : 'rendering' } TBAdminHeaderComponent >> renderButtonsOn: html [ - html form: [ self renderDisconnectButtonOn: html ] + html form: [ + self renderDisconnectButtonOn: html. + self renderPublicViewButton: html ] ] { #category : 'rendering' } TBAdminHeaderComponent >> renderDisconnectButtonOn: html [ - html formButton beSecondary; - callback: [ component goToPostListView ]; + html formButton + beSecondary; + callback: [ self session reset ]; with: [ html text: 'Disconnect'. 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' ] ] +] diff --git a/src/TinyBlog-Components/TBApplicationRootComponent.class.st b/src/TinyBlog-Components/TBApplicationRootComponent.class.st index 2883380..6c8d80c 100644 --- a/src/TinyBlog-Components/TBApplicationRootComponent.class.st +++ b/src/TinyBlog-Components/TBApplicationRootComponent.class.st @@ -18,6 +18,8 @@ TBApplicationRootComponent class >> initialize [ "self initialize" | app | app := WAAdmin register: self asApplicationAt: 'TinyBlog'. + app + preferenceAt: #sessionClass put: TBSession. app addLibrary: JQDeploymentLibrary; addLibrary: JQUiDeploymentLibrary; diff --git a/src/TinyBlog-Components/TBHeaderComponent.class.st b/src/TinyBlog-Components/TBHeaderComponent.class.st index 685c0dc..4a95f2a 100644 --- a/src/TinyBlog-Components/TBHeaderComponent.class.st +++ b/src/TinyBlog-Components/TBHeaderComponent.class.st @@ -36,7 +36,9 @@ TBHeaderComponent >> renderBrandOn: html [ { #category : 'rendering' } TBHeaderComponent >> renderButtonsOn: html [ - self renderModalLoginButtonOn: html + self session isLogged + ifTrue: [ self renderSimpleAdminButtonOn: html ] + ifFalse: [ self renderModalLoginButtonOn: html ] ] { #category : 'rendering' } @@ -63,3 +65,14 @@ TBHeaderComponent >> renderModalLoginButtonOn: html [ html span class: 'glyphicon glyphicon-lock'. 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' ]] +] diff --git a/src/TinyBlog-Components/TBPostsListComponent.class.st b/src/TinyBlog-Components/TBPostsListComponent.class.st index a78d964..82a9a73 100644 --- a/src/TinyBlog-Components/TBPostsListComponent.class.st +++ b/src/TinyBlog-Components/TBPostsListComponent.class.st @@ -134,7 +134,9 @@ TBPostsListComponent >> renderPostColumnOn: html [ { #category : 'as yet unclassified' } TBPostsListComponent >> tryConnectionWithLogin: login andPassword: password [ - (login = 'admin' and: [ password = 'topsecret' ]) - ifTrue: [ self goToAdministrationView ] + (login = self blog administrator login and: [ (SHA256 hashMessage: password) = self blog administrator password ]) + ifTrue: [ + self session currentAdmin: self blog administrator. + self goToAdministrationView ] ifFalse: [ self loginErrorOccurred ] ] diff --git a/src/TinyBlog-Components/TBSession.class.st b/src/TinyBlog-Components/TBSession.class.st new file mode 100644 index 0000000..f56c40b --- /dev/null +++ b/src/TinyBlog-Components/TBSession.class.st @@ -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 +] diff --git a/src/TinyBlog/TBAdministrator.class.st b/src/TinyBlog/TBAdministrator.class.st new file mode 100644 index 0000000..e1266ce --- /dev/null +++ b/src/TinyBlog/TBAdministrator.class.st @@ -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 +] diff --git a/src/TinyBlog/TBBlog.class.st b/src/TinyBlog/TBBlog.class.st index 4bcd338..d3c9eca 100644 --- a/src/TinyBlog/TBBlog.class.st +++ b/src/TinyBlog/TBBlog.class.st @@ -2,7 +2,8 @@ Class { #name : 'TBBlog', #superclass : 'Object', #instVars : [ - 'posts' + 'posts', + 'adminUser' ], #category : 'TinyBlog', #package : 'TinyBlog' @@ -38,6 +39,16 @@ TBBlog class >> current [ ifEmpty: [ self new save ] ] +{ #category : 'as yet unclassified' } +TBBlog class >> defaultAdminLogin [ + ^ 'admin' +] + +{ #category : 'as yet unclassified' } +TBBlog class >> defaultAdminPassword [ + ^ 'topsecret' +] + { #category : 'initialization' } TBBlog class >> initialize [ self reset @@ -59,6 +70,11 @@ TBBlog class >> reset [ self initializeVoyageOnMemoryDB. ] +{ #category : 'as yet unclassified' } +TBBlog >> administrator [ + ^ adminUser +] + { #category : 'reading' } TBBlog >> allBlogPosts [ ^ posts @@ -84,10 +100,18 @@ TBBlog >> allVisibleBlogPostsFromCategory: aCategory [ ^ 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' } TBBlog >> initialize [ super initialize. - posts := OrderedCollection new + posts := OrderedCollection new. + adminUser := self createAdministrator ] { #category : 'deleting' }