We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
`Insufficient Permission for Chat Profile` on load
Use Case:

1. Create New User without System Manager Role
2. Log-in to System
3. Error Pops-up when system is trying to create Chat Profile.

I tried replicating it to a user that was once has a System Manager role and it was not popping up. It was because the user has already Chat Profile.

Here is the log. Hope this helps!

![profile](https://user-images.githubusercontent.com/21003054/35091472-ba84aa68-fc77-11e7-860e-789961c1a09e.png)


Any help is greatly appreciated. I will also try to create an issue for this here in discuss.

Thanks,

Dori

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `frappe/chat/doctype/chat_profile/chat_profile.py`
Content:
```
1 # imports - module imports
2 from   frappe.model.document import Document
3 from   frappe import _
4 import frappe
5 
6 # imports - frappe module imports
7 from   frappe.core.doctype.version.version import get_diff
8 from   frappe.chat.doctype.chat_room       import chat_room
9 from   frappe.chat.util import (
10     safe_json_loads,
11     filter_dict,
12     dictify
13 )
14 
15 session = frappe.session
16 
17 class ChatProfile(Document):
18     def before_save(self):
19         if not self.is_new():
20             self.get_doc_before_save()
21 
22     def on_update(self):
23         if not self.is_new():
24             b, a = self.get_doc_before_save(), self
25             diff = dictify(get_diff(a, b))
26             if diff:
27                 user   = session.user
28 
29                 fields = [changed[0] for changed in diff.changed]
30 
31                 if 'status' in fields:
32                     rooms  = chat_room.get(user, filters = ['Chat Room', 'type', '=', 'Direct'])
33                     update = dict(user = user, data = dict(status = self.status))
34 
35                     for room in rooms:
36                         frappe.publish_realtime('frappe.chat.profile:update', update, room = room.name, after_commit = True)
37 
38                 if 'enable_chat' in fields:
39                     update = dict(user = user, data = dict(enable_chat = bool(self.enable_chat)))
40                     frappe.publish_realtime('frappe.chat.profile:update', update, user = user, after_commit = True)
41 
42 def authenticate(user):
43     if user != session.user:
44         frappe.throw(_("Sorry, you're not authorized."))
45 
46 @frappe.whitelist()
47 def get(user, fields = None):
48     duser   = frappe.get_doc('User', user)
49     dprof   = frappe.get_doc('Chat Profile', user)
50 
51     # If you're adding something here, make sure the client recieves it.
52     profile = dict(
53         # User
54         name       = duser.name,
55         email      = duser.email,
56         first_name = duser.first_name,
57         last_name  = duser.last_name,
58         username   = duser.username,
59         avatar     = duser.user_image,
60         bio        = duser.bio,
61         # Chat Profile
62         status             = dprof.status,
63         chat_background    = dprof.chat_background,
64         message_preview    = bool(dprof.message_preview),
65         notification_tones = bool(dprof.notification_tones),
66         conversation_tones = bool(dprof.conversation_tones),
67         enable_chat        = bool(dprof.enable_chat)
68     )
69     profile = filter_dict(profile, fields)
70 
71     return dictify(profile)
72 
73 @frappe.whitelist()
74 def create(user, exists_ok = False, fields = None):
75     authenticate(user)
76 
77     exists_ok, fields = safe_json_loads(exists_ok, fields)
78 
79     if frappe.db.exists('Chat Profile', user):
80         if not exists_ok:
81             frappe.throw(_('Chat Profile for User {user} exists.'.format(user = user)))
82     else:
83         dprof      = frappe.new_doc('Chat Profile')
84         dprof.user = user
85         dprof.save()
86 
87     profile = get(user, fields = fields)
88 
89     return profile
90 
91 @frappe.whitelist()
92 def update(user, data):
93     authenticate(user)
94 
95     data  = safe_json_loads(data)
96 
97     dprof = frappe.get_doc('Chat Profile', user)
98     dprof.update(data)
99     dprof.save(ignore_permissions = True)
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/frappe/chat/doctype/chat_profile/chat_profile.py b/frappe/chat/doctype/chat_profile/chat_profile.py
--- a/frappe/chat/doctype/chat_profile/chat_profile.py
+++ b/frappe/chat/doctype/chat_profile/chat_profile.py
@@ -82,7 +82,7 @@
     else:
         dprof      = frappe.new_doc('Chat Profile')
         dprof.user = user
-        dprof.save()
+        dprof.save(ignore_permissions = True)
 
     profile = get(user, fields = fields)

{"golden_diff": "diff --git a/frappe/chat/doctype/chat_profile/chat_profile.py b/frappe/chat/doctype/chat_profile/chat_profile.py\n--- a/frappe/chat/doctype/chat_profile/chat_profile.py\n+++ b/frappe/chat/doctype/chat_profile/chat_profile.py\n@@ -82,7 +82,7 @@\n     else:\n         dprof      = frappe.new_doc('Chat Profile')\n         dprof.user = user\n-        dprof.save()\n+        dprof.save(ignore_permissions = True)\n \n     profile = get(user, fields = fields)\n", "issue": "`Insufficient Permission for Chat Profile` on load\nUse Case:\r\n\r\n1. Create New User without System Manager Role\r\n2. Log-in to System\r\n3. Error Pops-up when system is trying to create Chat Profile.\r\n\r\nI tried replicating it to a user that was once has a System Manager role and it was not popping up. It was because the user has already Chat Profile.\r\n\r\nHere is the log. Hope this helps!\r\n\r\n![profile](https://user-images.githubusercontent.com/21003054/35091472-ba84aa68-fc77-11e7-860e-789961c1a09e.png)\r\n\r\n\r\nAny help is greatly appreciated. I will also try to create an issue for this here in discuss.\r\n\r\nThanks,\r\n\r\nDori\n", "before_files": [{"content": "# imports - module imports\nfrom   frappe.model.document import Document\nfrom   frappe import _\nimport frappe\n\n# imports - frappe module imports\nfrom   frappe.core.doctype.version.version import get_diff\nfrom   frappe.chat.doctype.chat_room       import chat_room\nfrom   frappe.chat.util import (\n    safe_json_loads,\n    filter_dict,\n    dictify\n)\n\nsession = frappe.session\n\nclass ChatProfile(Document):\n    def before_save(self):\n        if not self.is_new():\n            self.get_doc_before_save()\n\n    def on_update(self):\n        if not self.is_new():\n            b, a = self.get_doc_before_save(), self\n            diff = dictify(get_diff(a, b))\n            if diff:\n                user   = session.user\n\n                fields = [changed[0] for changed in diff.changed]\n\n                if 'status' in fields:\n                    rooms  = chat_room.get(user, filters = ['Chat Room', 'type', '=', 'Direct'])\n                    update = dict(user = user, data = dict(status = self.status))\n\n                    for room in rooms:\n                        frappe.publish_realtime('frappe.chat.profile:update', update, room = room.name, after_commit = True)\n\n                if 'enable_chat' in fields:\n                    update = dict(user = user, data = dict(enable_chat = bool(self.enable_chat)))\n                    frappe.publish_realtime('frappe.chat.profile:update', update, user = user, after_commit = True)\n\ndef authenticate(user):\n    if user != session.user:\n        frappe.throw(_(\"Sorry, you're not authorized.\"))\n\n@frappe.whitelist()\ndef get(user, fields = None):\n    duser   = frappe.get_doc('User', user)\n    dprof   = frappe.get_doc('Chat Profile', user)\n\n    # If you're adding something here, make sure the client recieves it.\n    profile = dict(\n        # User\n        name       = duser.name,\n        email      = duser.email,\n        first_name = duser.first_name,\n        last_name  = duser.last_name,\n        username   = duser.username,\n        avatar     = duser.user_image,\n        bio        = duser.bio,\n        # Chat Profile\n        status             = dprof.status,\n        chat_background    = dprof.chat_background,\n        message_preview    = bool(dprof.message_preview),\n        notification_tones = bool(dprof.notification_tones),\n        conversation_tones = bool(dprof.conversation_tones),\n        enable_chat        = bool(dprof.enable_chat)\n    )\n    profile = filter_dict(profile, fields)\n\n    return dictify(profile)\n\n@frappe.whitelist()\ndef create(user, exists_ok = False, fields = None):\n    authenticate(user)\n\n    exists_ok, fields = safe_json_loads(exists_ok, fields)\n\n    if frappe.db.exists('Chat Profile', user):\n        if not exists_ok:\n            frappe.throw(_('Chat Profile for User {user} exists.'.format(user = user)))\n    else:\n        dprof      = frappe.new_doc('Chat Profile')\n        dprof.user = user\n        dprof.save()\n\n    profile = get(user, fields = fields)\n\n    return profile\n\n@frappe.whitelist()\ndef update(user, data):\n    authenticate(user)\n\n    data  = safe_json_loads(data)\n\n    dprof = frappe.get_doc('Chat Profile', user)\n    dprof.update(data)\n    dprof.save(ignore_permissions = True)", "path": "frappe/chat/doctype/chat_profile/chat_profile.py"}], "after_files": [{"content": "# imports - module imports\nfrom   frappe.model.document import Document\nfrom   frappe import _\nimport frappe\n\n# imports - frappe module imports\nfrom   frappe.core.doctype.version.version import get_diff\nfrom   frappe.chat.doctype.chat_room       import chat_room\nfrom   frappe.chat.util import (\n    safe_json_loads,\n    filter_dict,\n    dictify\n)\n\nsession = frappe.session\n\nclass ChatProfile(Document):\n    def before_save(self):\n        if not self.is_new():\n            self.get_doc_before_save()\n\n    def on_update(self):\n        if not self.is_new():\n            b, a = self.get_doc_before_save(), self\n            diff = dictify(get_diff(a, b))\n            if diff:\n                user   = session.user\n\n                fields = [changed[0] for changed in diff.changed]\n\n                if 'status' in fields:\n                    rooms  = chat_room.get(user, filters = ['Chat Room', 'type', '=', 'Direct'])\n                    update = dict(user = user, data = dict(status = self.status))\n\n                    for room in rooms:\n                        frappe.publish_realtime('frappe.chat.profile:update', update, room = room.name, after_commit = True)\n\n                if 'enable_chat' in fields:\n                    update = dict(user = user, data = dict(enable_chat = bool(self.enable_chat)))\n                    frappe.publish_realtime('frappe.chat.profile:update', update, user = user, after_commit = True)\n\ndef authenticate(user):\n    if user != session.user:\n        frappe.throw(_(\"Sorry, you're not authorized.\"))\n\n@frappe.whitelist()\ndef get(user, fields = None):\n    duser   = frappe.get_doc('User', user)\n    dprof   = frappe.get_doc('Chat Profile', user)\n\n    # If you're adding something here, make sure the client recieves it.\n    profile = dict(\n        # User\n        name       = duser.name,\n        email      = duser.email,\n        first_name = duser.first_name,\n        last_name  = duser.last_name,\n        username   = duser.username,\n        avatar     = duser.user_image,\n        bio        = duser.bio,\n        # Chat Profile\n        status             = dprof.status,\n        chat_background    = dprof.chat_background,\n        message_preview    = bool(dprof.message_preview),\n        notification_tones = bool(dprof.notification_tones),\n        conversation_tones = bool(dprof.conversation_tones),\n        enable_chat        = bool(dprof.enable_chat)\n    )\n    profile = filter_dict(profile, fields)\n\n    return dictify(profile)\n\n@frappe.whitelist()\ndef create(user, exists_ok = False, fields = None):\n    authenticate(user)\n\n    exists_ok, fields = safe_json_loads(exists_ok, fields)\n\n    if frappe.db.exists('Chat Profile', user):\n        if not exists_ok:\n            frappe.throw(_('Chat Profile for User {user} exists.'.format(user = user)))\n    else:\n        dprof      = frappe.new_doc('Chat Profile')\n        dprof.user = user\n        dprof.save(ignore_permissions = True)\n\n    profile = get(user, fields = fields)\n\n    return profile\n\n@frappe.whitelist()\ndef update(user, data):\n    authenticate(user)\n\n    data  = safe_json_loads(data)\n\n    dprof = frappe.get_doc('Chat Profile', user)\n    dprof.update(data)\n    dprof.save(ignore_permissions = True)", "path": "frappe/chat/doctype/chat_profile/chat_profile.py"}]}